phymodel_server.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * \file phy_model_server.c
  3. *
  4. * \brief This file defines the Mesh Configuration Model Application Interface
  5. * - includes Data Structures and Methods for both Server and Client.
  6. */
  7. /*
  8. * Copyright (C) 2020. phyplus Ltd.
  9. * All rights reserved.
  10. */
  11. /* --------------------------------------------- Header File Inclusion */
  12. #include "phymodel_server.h"
  13. /* --------------------------------------------- Data Types/ Structures */
  14. /* --------------------------------------------- Global Definitions */
  15. /* --------------------------------------------- Static Global Variables */
  16. static DECL_CONST UINT32 phy_model_server_opcode_list[] =
  17. {
  18. MS_ACCESS_PHY_MODEL_GET_OPCODE,
  19. MS_ACCESS_PHY_MODEL_SET_OPCODE,
  20. MS_ACCESS_PHY_MODEL_SET_UNACKNOWLEDGED_OPCODE,
  21. MS_ACCESS_PHY_MODEL_WRITECMD_OPCODE,
  22. MS_ACCESS_PHY_MODEL_CONFIRMATION_OPCODE,
  23. MS_ACCESS_PHY_MODEL_NOTIFY_OPCODE
  24. };
  25. //static MS_ACCESS_MODEL_HANDLE phy_model_server_model_handle;
  26. static MS_PHY_MODEL_SERVER_CB phy_model_server_UI_cb;
  27. /* ----------------------------------------- Functions */
  28. /**
  29. * \brief API to send reply or to update state change
  30. *
  31. * \par Description
  32. * This is to send reply for a request or to inform change in state.
  33. *
  34. * \param [in] ctx Context of the message.
  35. * \param [in] current_state_params Model specific current state parameters.
  36. * \param [in] target_state_params Model specific target state parameters (NULL: to be ignored).
  37. * \param [in] remaining_time Time from current state to target state (0: to be ignored).
  38. * \param [in] ext_params Additional parameters (NULL: to be ignored).
  39. *
  40. * \return API_SUCCESS or an error code indicating reason for failure
  41. */
  42. API_RESULT MS_phy_model_server_state_update
  43. (
  44. /* IN */ MS_ACCESS_MODEL_REQ_MSG_CONTEXT * ctx,
  45. /* IN */ MS_ACCESS_PHY_MODEL_STATE_PARAMS * current_state_params,
  46. /* IN */ MS_ACCESS_MODEL_STATE_PARAMS * target_state_params,
  47. /* IN */ UINT16 remaining_time,
  48. /* IN */ MS_ACCESS_MODEL_EXT_PARAMS * ext_params,
  49. /* IN */ UINT16 data_length
  50. )
  51. {
  52. API_RESULT retval;
  53. /* TODO: Check what should be maximum length */
  54. UCHAR buffer[256];
  55. UCHAR * pdu_ptr;
  56. UINT16 marker;
  57. UINT32 opcode;
  58. // UINT16 ttl;
  59. // ACCESS_CM_GET_RX_TTL(ttl);
  60. retval = API_FAILURE;
  61. marker = 0;
  62. // printf(
  63. // "current_state_params->state_type 0x%04X.\n",current_state_params->phy_mode_type);
  64. switch (current_state_params->phy_mode_type)
  65. {
  66. case MS_STATE_PHY_MODEL_ONOFF_T:
  67. {
  68. buffer[marker] = ++vendor_tid;
  69. marker++;
  70. MS_PACK_LE_2_BYTE_VAL(&buffer[marker], current_state_params->phy_mode_type);
  71. marker += 2;
  72. EM_mem_copy(&buffer[marker], current_state_params->phy_mode_param, 1);
  73. marker++;
  74. /* Set Opcode */
  75. opcode = MS_ACCESS_PHY_MODEL_STATUS_OPCODE;
  76. }
  77. break;
  78. case MS_STATE_PHYMODEL_NOTIFY_T:
  79. {
  80. EM_mem_copy(&buffer[marker], current_state_params->phy_mode_param, data_length);
  81. marker += data_length;
  82. /* Set Opcode */
  83. opcode = MS_ACCESS_PHY_MODEL_NOTIFY_OPCODE;
  84. }
  85. break;
  86. default:
  87. break;
  88. }
  89. /* Publish - reliable */
  90. if (0 == marker)
  91. {
  92. pdu_ptr = NULL;
  93. }
  94. else
  95. {
  96. pdu_ptr = buffer;
  97. }
  98. cfg_retry_flag = 1;
  99. retval = MS_access_reply
  100. (
  101. &ctx->handle,
  102. ctx->daddr,
  103. ctx->saddr,
  104. ctx->subnet_handle,
  105. ctx->appkey_handle,
  106. ACCESS_INVALID_DEFAULT_TTL,
  107. opcode,
  108. pdu_ptr,
  109. marker
  110. );
  111. return retval;
  112. }
  113. /**
  114. * \brief Access Layer Application Asynchronous Notification Callback.
  115. *
  116. * \par Description
  117. * Access Layer calls the registered callback to indicate events occurred to the application.
  118. *
  119. * \param [in] handle Model Handle.
  120. * \param [in] saddr 16 bit Source Address.
  121. * \param [in] daddr 16 bit Destination Address.
  122. * \param [in] appkey_handle AppKey Handle.
  123. * \param [in] subnet_handle Subnet Handle.
  124. * \param [in] opcode Opcode.
  125. * \param [in] data_param Data associated with the event if any or NULL.
  126. * \param [in] data_len Size of the event data. 0 if event data is NULL.
  127. */
  128. API_RESULT phy_model_server_cb
  129. (
  130. /* IN */ MS_ACCESS_MODEL_HANDLE * handle,
  131. /* IN */ MS_NET_ADDR saddr,
  132. /* IN */ MS_NET_ADDR daddr,
  133. /* IN */ MS_SUBNET_HANDLE subnet_handle,
  134. /* IN */ MS_APPKEY_HANDLE appkey_handle,
  135. /* IN */ UINT32 opcode,
  136. /* IN */ UCHAR * data_param,
  137. /* IN */ UINT16 data_len
  138. )
  139. {
  140. MS_ACCESS_MODEL_REQ_MSG_CONTEXT req_context;
  141. MS_ACCESS_MODEL_REQ_MSG_RAW req_raw;
  142. MS_ACCESS_MODEL_REQ_MSG_T req_type;
  143. MS_ACCESS_MODEL_EXT_PARAMS * ext_params_p;
  144. MS_ACCESS_PHY_MODEL_STATE_PARAMS state_params;
  145. UINT16 marker;
  146. API_RESULT retval;
  147. retval = API_SUCCESS;
  148. ext_params_p = NULL;
  149. marker = 0;
  150. /* Request Context */
  151. req_context.handle = *handle;
  152. req_context.saddr = saddr;
  153. req_context.daddr = daddr;
  154. req_context.subnet_handle = subnet_handle;
  155. req_context.appkey_handle = appkey_handle;
  156. /* Request Raw */
  157. req_raw.opcode = opcode;
  158. req_raw.data_param = data_param;
  159. req_raw.data_len = data_len;
  160. state_params.phy_mode_param = NULL;
  161. switch(opcode)
  162. {
  163. case MS_ACCESS_PHY_MODEL_GET_OPCODE:
  164. {
  165. // printf(
  166. // "MS_ACCESS_PHY_MODEL_GET_OPCODE\n");
  167. MODEL_OPCODE_HANDLER_CALL(vendor_example_get_handler);
  168. marker = 1;
  169. MS_UNPACK_LE_2_BYTE(&state_params.phy_mode_type, data_param+marker);
  170. marker += 2;
  171. /* Get Request Type */
  172. req_type.type = MS_ACCESS_MODEL_REQ_MSG_T_GET;
  173. req_type.to_be_acked = 0x01;
  174. /* Assign reqeusted state type to the application */
  175. }
  176. break;
  177. case MS_ACCESS_PHY_MODEL_SET_OPCODE:
  178. case MS_ACCESS_PHY_MODEL_SET_UNACKNOWLEDGED_OPCODE:
  179. {
  180. // printf(
  181. // "MS_ACCESS_PHY_MODEL_SET_OPCODE\n");
  182. MODEL_OPCODE_HANDLER_CALL(vendor_example_set_handler);
  183. marker = 1;
  184. MS_UNPACK_LE_2_BYTE(&state_params.phy_mode_type, data_param+marker);
  185. marker += 2;
  186. state_params.phy_mode_param = &data_param[marker];
  187. /* Set Request Type */
  188. req_type.type = MS_ACCESS_MODEL_REQ_MSG_T_SET;
  189. if(MS_ACCESS_PHY_MODEL_SET_OPCODE == opcode)
  190. {
  191. req_type.to_be_acked = 0x01;
  192. }
  193. else
  194. {
  195. req_type.to_be_acked = 0x00;
  196. }
  197. }
  198. break;
  199. case MS_ACCESS_PHY_MODEL_STATUS_OPCODE:
  200. {
  201. // printf(
  202. // "MS_ACCESS_PHY_MODEL_STATUS\n");
  203. MODEL_OPCODE_HANDLER_CALL(vendor_example_status_handler);
  204. /* Set Request Type */
  205. req_type.type = MS_ACCESS_MODEL_REQ_MSG_T_OTHERS;
  206. req_type.to_be_acked = 0x00;
  207. }
  208. break;
  209. case MS_ACCESS_PHY_MODEL_CONFIRMATION_OPCODE:
  210. {
  211. // printf(
  212. // "MS_ACCESS_PHY_MODEL_CONFIRMATION\n");
  213. MODEL_OPCODE_HANDLER_CALL(vendor_example_confirmation_handler);
  214. /* Set Request Type */
  215. req_type.type = MS_ACCESS_MODEL_REQ_MSG_T_OTHERS;
  216. req_type.to_be_acked = 0x00;
  217. }
  218. break;
  219. case MS_ACCESS_PHY_MODEL_WRITECMD_OPCODE:
  220. {
  221. // printf(
  222. // "MS_ACCESS_PHY_MODEL_WRITECMD_OPCODE\n");
  223. marker = 1;
  224. MS_UNPACK_LE_2_BYTE(&state_params.phy_mode_type, data_param+marker);
  225. marker += 2;
  226. state_params.phy_mode_param = &data_param[marker];
  227. /* Set Request Type */
  228. req_type.type = MS_ACCESS_MODEL_REQ_MSG_T_OTHERS;
  229. req_type.to_be_acked = 0x00;
  230. }
  231. break;
  232. case MS_ACCESS_PHY_MODEL_NOTIFY_OPCODE:
  233. {
  234. state_params.phy_mode_type = MS_STATE_PHYMODEL_NOTIFY_T;
  235. marker = 1;
  236. state_params.phy_mode_param = &data_param[marker];
  237. /* Set Request Type */
  238. req_type.type = MS_ACCESS_MODEL_REQ_MSG_T_OTHERS;
  239. req_type.to_be_acked = 0x00;
  240. }
  241. break;
  242. default:
  243. // printf(
  244. // "MS_ACCESS_PHY_MODEL_NONE_OPCODE\n");
  245. break;
  246. }
  247. /* Application callback */
  248. if (NULL != phy_model_server_UI_cb)
  249. {
  250. phy_model_server_UI_cb(&req_context, &req_raw, &req_type, &state_params, ext_params_p);
  251. }
  252. return retval;
  253. }
  254. /**
  255. * \brief API to initialize Vendor_Example_1 Server model
  256. *
  257. * \par Description
  258. * This is to initialize Vendor_Example_1 Server model and to register with Acess layer.
  259. *
  260. * \param [in] element_handle
  261. * Element identifier to be associated with the model instance.
  262. *
  263. * \param [in, out] model_handle
  264. * Model identifier associated with the model instance on successful initialization.
  265. * After power cycle of an already provisioned node, the model handle will have
  266. * valid value and the same will be reused for registration.
  267. *
  268. * \param [in] UI_cb Application Callback to be used by the Vendor_Example_1 Server.
  269. *
  270. * \return API_SUCCESS or an error code indicating reason for failure
  271. */
  272. API_RESULT MS_phy_model_server_init
  273. (
  274. /* IN */ MS_ACCESS_ELEMENT_HANDLE element_handle,
  275. /* INOUT */ MS_ACCESS_MODEL_HANDLE * model_handle,
  276. /* IN */ MS_PHY_MODEL_SERVER_CB UI_cb
  277. )
  278. {
  279. API_RESULT retval;
  280. MS_ACCESS_NODE_ID node_id;
  281. MS_ACCESS_MODEL model;
  282. /* TBD: Initialize MUTEX and other data structures */
  283. /* Using default node ID */
  284. node_id = MS_ACCESS_DEFAULT_NODE_ID;
  285. /* Configure Model */
  286. model.model_id.id = MS_MODEL_ID_PHY_MODEL_SERVER;
  287. model.model_id.type = MS_ACCESS_MODEL_TYPE_VENDOR;
  288. model.elem_handle = element_handle;
  289. /* Register Callback */
  290. model.cb = phy_model_server_cb;
  291. /* List of Opcodes */
  292. model.opcodes = phy_model_server_opcode_list;
  293. model.num_opcodes = sizeof(phy_model_server_opcode_list) / sizeof(UINT32);
  294. retval = MS_access_register_model
  295. (
  296. node_id,
  297. &model,
  298. model_handle
  299. );
  300. /* Save Application Callback */
  301. phy_model_server_UI_cb = UI_cb;
  302. // /* TODO: Remove */
  303. // phy_model_server_model_handle = *model_handle;
  304. return retval;
  305. }