p≡p engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
9.0 KiB

  1. // This file is under GNU General Public License 3.0
  2. // see LICENSE.txt
  3. // generate actions skeleton
  4. // Copyleft (c) 2016, p≡p foundation
  5. // Written by Volker Birk
  6. include yslt.yml2
  7. tstylesheet {
  8. include standardlib.ysl2
  9. include ./functions.ysl2
  10. template "/protocol" {
  11. apply "fsm", mode=send, 0;
  12. apply "fsm", mode=other, 0;
  13. }
  14. template "fsm", mode=send document "generated/{@filename}_send_actions.c", "text" {
  15. const "name", "@name";
  16. const "filename", "@filename";
  17. ||
  18. // Send Actions for «@name» state machine
  19. #include <assert.h>
  20. #include "pEp_internal.h"
  21. #include "keymanagement.h"
  22. #include "message.h"
  23. #include "«@filename»_fsm.h"
  24. #include "baseprotocol.h"
  25. #include "map_asn1.h"
  26. #include "../asn.1/DeviceGroup-Protocol.h"
  27. #include "sync_impl.h"
  28. ||
  29. for "func:distinctName(//action)"
  30. if "substring(@name, 1, 4) = 'send'"
  31. | #include "../asn.1/«substring(@name, 5, 255)».h"
  32. |
  33. for "func:distinctName(//action)"
  34. if "substring(@name, 1, 4) = 'send'"
  35. call "send_action"
  36. with "action", ".",
  37. with "fsm", "$name",
  38. with "filename", "$filename";
  39. ||
  40. PEP_STATUS _notifyHandshake(
  41. PEP_SESSION session,
  42. Identity partner,
  43. sync_handshake_signal signal
  44. );
  45. ||
  46. for "func:distinctName(//action)"
  47. if "substring(@name, 1, 6) = 'notify'"
  48. call "notify_action"
  49. with "action", ".",
  50. with "fsm", "$name",
  51. with "filename", "$filename";
  52. }
  53. template "fsm", mode=other document "skeletons/{@filename}_actions.c", "text" {
  54. const "name", "@name";
  55. const "filename", "@filename";
  56. ||
  57. // Actions for «@name» state machine
  58. #include <assert.h>
  59. #include "pEp_internal.h"
  60. #include "keymanagement.h"
  61. #include "message.h"
  62. #include "«@filename»_fsm.h"
  63. #include "../asn.1/DeviceGroup-Protocol.h"
  64. ||
  65. for "func:distinctName(//action)"
  66. if "substring(@name, 1, 4) != 'send'"
  67. call "other_action"
  68. with "action", ".",
  69. with "fsm", "$name",
  70. with "filename", "$filename";
  71. }
  72. function "paramcheck" {
  73. param "partner";
  74. |> assert(session);
  75. choose {
  76. when "$partner"
  77. ||
  78. assert(partner);
  79. if (!(session && partner))
  80. return PEP_ILLEGAL_VALUE;
  81. ||
  82. otherwise
  83. ||
  84. assert(!partner);
  85. if (!(session && !partner))
  86. return PEP_ILLEGAL_VALUE;
  87. ||
  88. }
  89. }
  90. function "other_action" {
  91. param "action";
  92. param "fsm";
  93. param "filename", "'###'";
  94. ||
  95. // «$action/@name»() -
  96. //
  97. // params:
  98. // session (in) session handle
  99. // state (in) state the state machine is in
  100. `` if "parm" | // partner (in) partner to communicate with
  101. `` if "not(parm)" | // partner (in) (must be NULL)
  102. //
  103. // returns:
  104. // PEP_STATUS_OK or any other value on error
  105. PEP_STATUS «$action/@name»(
  106. PEP_SESSION session,
  107. «$fsm»_state state,
  108. Identity partner,
  109. void *extra
  110. )
  111. {
  112. PEP_STATUS status = PEP_STATUS_OK;
  113. `` call "paramcheck" with "partner", "parm/partner";
  114. // working code
  115. // free extra
  116. return status;
  117. enomem:
  118. status = PEP_OUT_OF_MEMORY;
  119. error:
  120. // free extra
  121. return status;
  122. }
  123. ||
  124. }
  125. function "send_action" {
  126. param "action";
  127. param "fsm";
  128. param "filename", "'###'";
  129. const "name", "substring($action/@name, 5, 255)";
  130. const "lname", "concat(yml:lcase(substring($name, 1, 1)), substring($name, 2))";
  131. ||
  132. // «$action/@name»() - send «$name» message
  133. //
  134. // params:
  135. // session (in) session handle
  136. // state (in) state the state machine is in
  137. `` if "parm" | // partner (in) partner to communicate with
  138. `` if "not(parm)" | // partner (in) (must be NULL)
  139. //
  140. // returns:
  141. // PEP_STATUS_OK or any other value on error
  142. PEP_STATUS «$action/@name»(
  143. PEP_SESSION session,
  144. «$fsm»_state state,
  145. Identity partner,
  146. void *extra
  147. )
  148. {
  149. assert(session && state);
  150. if (!(session && state))
  151. return PEP_ILLEGAL_VALUE;
  152. PEP_STATUS status = PEP_STATUS_OK;
  153. `` if "$name='GroupKeys' or $name='GroupUpdate'" |> identity_list *kl = new_identity_list(NULL);
  154. DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_«$lname»);
  155. if (!msg)
  156. goto enomem;
  157. ||
  158. choose {
  159. when "$name='GroupKeys' or $name='GroupUpdate'" {
  160. |
  161. |> status = _own_identities_retrieve(session, &kl, PEP_idf_not_for_sync);
  162. |> if (status != PEP_STATUS_OK)
  163. |>> goto error;
  164. |> if (IdentityList_from_identity_list(kl, &msg->payload.choice.«$lname».ownIdentities) == NULL)
  165. |>> goto enomem;
  166. }
  167. }
  168. choose {
  169. when "$name='GroupKeys' or $name='HandshakeRequest'" {
  170. |
  171. |> msg->payload.choice.«$lname».partner_id =
  172. |> OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
  173. |> partner->user_id, -1);
  174. |> if (partner->user_id && !msg->payload.choice.«$lname».partner_id)
  175. |> goto enomem;
  176. |
  177. |> char *devgrp = NULL;
  178. |> status = get_device_group(session, &devgrp);
  179. |> if (status == PEP_STATUS_OK && devgrp && devgrp[0])
  180. |> msg->payload.choice.«$lname».group_id =
  181. |> OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
  182. |> devgrp, -1);
  183. |> free(devgrp);
  184. |> if (devgrp && !msg->payload.choice.«$lname».partner_id)
  185. |> goto enomem;
  186. }
  187. }
  188. ||
  189. ||
  190. choose {
  191. when "count(/protocol/unencrypted/*[name()=$action/@name]) = 0"
  192. |> bool encrypted = true;
  193. otherwise
  194. |> bool encrypted = false;
  195. }
  196. choose {
  197. when "count(/protocol/broadcast/*[name()=$action/@name]) = 0"
  198. |> status = unicast_msg(session, partner, state, msg, encrypted);
  199. otherwise
  200. |> status = multicast_self_msg(session, state, msg, encrypted);
  201. }
  202. ||
  203. if (status != PEP_STATUS_OK)
  204. goto error;
  205. `` if "$name='GroupKeys' or $name='GroupUpdate'" |> free_identity_list(kl);
  206. free_DeviceGroup_Protocol_msg(msg);
  207. return PEP_STATUS_OK;
  208. enomem:
  209. status = PEP_OUT_OF_MEMORY;
  210. error:
  211. free_DeviceGroup_Protocol_msg(msg);
  212. `` if "$name='GroupKeys'" |> free_identity_list(kl);
  213. return status;
  214. }
  215. ||
  216. }
  217. function "UnCamelUp" {
  218. param "text";
  219. const "tokens", "str:tokenize($text, '')";
  220. for "$tokens" {
  221. choose {
  222. when "contains('ABCDEFGHIJKLMNOPQRSTUVWXYZ',.)" > _«.»
  223. otherwise value "yml:ucase(.)";
  224. }
  225. }
  226. }
  227. function "notify_action" {
  228. param "action";
  229. param "fsm";
  230. param "filename", "'###'";
  231. const "name", "substring($action/@name, 7, 255)";
  232. const "uname" call "UnCamelUp" with "text", "$name";
  233. ||
  234. // «$action/@name»() - notify «$name» to app
  235. //
  236. // params:
  237. // session (in) session handle
  238. // state (in) state the state machine is in
  239. // partner (in) partner to communicate with
  240. //
  241. // returns:
  242. // PEP_STATUS_OK or any other value on error
  243. PEP_STATUS «$action/@name»(
  244. PEP_SESSION session,
  245. «$fsm»_state state,
  246. Identity partner,
  247. void *extra
  248. )
  249. {
  250. assert(session && state);
  251. assert(extra == NULL);
  252. if (!(session && state && extra == NULL))
  253. return PEP_ILLEGAL_VALUE;
  254. return _notifyHandshake(session, partner, SYNC_NOTIFY«$uname»);
  255. }
  256. ||
  257. }
  258. }