joylink_sample_band.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <rtthread.h>
  5. #include <arpa/inet.h>
  6. #include <netdev.h>
  7. #include <cJSON.h>
  8. #include <joylink.h>
  9. #include <joylink_log.h>
  10. #include <joylink_dev.h>
  11. #include <joylink_extern.h>
  12. #include <joylink_extern.h>
  13. #include <joylink_porting_layer.h>
  14. #define USER_DATA_SPORT "Sport"
  15. #define USER_DATA_SLEEP "Sleep"
  16. #define USER_DATA_PHYSIQUE "Physique"
  17. #define USER_DATA_CMD_SPORT 0x01
  18. #define USER_DATA_CMD_SLEEP 0x02
  19. #define USER_DATA_CMD_PHYSIQUE 0x03
  20. #define USER_DATT_LEN 128
  21. typedef struct _user_dev_status_t {
  22. char Sport[USER_DATT_LEN];
  23. char Sleep[USER_DATT_LEN];
  24. char Physique[USER_DATT_LEN];
  25. } user_dev_status_t;
  26. static user_dev_status_t user_dev;
  27. extern struct netdev *netdev_default;
  28. /* Check device network is connected */
  29. int joylink_dev_is_net_ok(void)
  30. {
  31. return (netdev_is_link_up(netdev_default) && (!ip_addr_isany(&(netdev_default->ip_addr)))) ? 1 : 0;
  32. }
  33. /* Get device UUID */
  34. int joylink_dev_get_uuid(char *out)
  35. {
  36. rt_memcpy(out, JOYLINK_SAMPLE_UUID, rt_strlen(JOYLINK_SAMPLE_UUID));
  37. return RT_EOK;
  38. }
  39. /* Get devuce public key */
  40. int joylink_dev_get_public_key(char *out)
  41. {
  42. rt_memcpy(out, JOYLINK_SAMPLE_PUB_KEY, rt_strlen(JOYLINK_SAMPLE_PUB_KEY));
  43. return RT_EOK;
  44. }
  45. /* Get device MAC address */
  46. int joylink_dev_get_user_mac(char *out)
  47. {
  48. int idx = 0;
  49. while(!netdev_is_up(netdev_default))
  50. {
  51. // delay for network interface device
  52. rt_thread_mdelay(500);
  53. }
  54. for (idx = 0; idx < netdev_default->hwaddr_len; idx++)
  55. {
  56. rt_snprintf(out + 2 * idx, 16 - 2 * idx, "%02X", netdev_default->hwaddr[idx]);
  57. }
  58. return RT_EOK;
  59. }
  60. /* Get device pricate key */
  61. int joylink_dev_get_private_key(char *out)
  62. {
  63. rt_memcpy(out, JOYLINK_SAMPLE_PRIVATE_KEY, rt_strlen(JOYLINK_SAMPLE_PRIVATE_KEY));
  64. return RT_EOK;
  65. }
  66. /* Get device usert data */
  67. user_dev_status_t *joylink_dev_user_data_get(void)
  68. {
  69. return (user_dev_status_t *) &user_dev;
  70. }
  71. /* Set device user data information */
  72. int joylink_dev_user_data_set(int cmd, void *value)
  73. {
  74. user_dev_status_t *status = &user_dev;
  75. char *data = (void *)value;
  76. switch (cmd)
  77. {
  78. case USER_DATA_CMD_SPORT:
  79. rt_memset(status->Sport, 0, USER_DATT_LEN);
  80. rt_memcpy(status->Sport, data, rt_strlen(data));
  81. log_debug("set wristband device sport status(%s).", status->Sport);
  82. break;
  83. case USER_DATA_CMD_SLEEP:
  84. rt_memset(status->Sleep, 0, USER_DATT_LEN);
  85. rt_memcpy(status->Sleep, data, rt_strlen(data));
  86. log_debug("set wristband device sleep status(%s).", status->Sleep);
  87. break;
  88. case USER_DATA_CMD_PHYSIQUE:
  89. rt_memset(status->Physique, 0, USER_DATT_LEN);
  90. rt_memcpy(status->Physique, data, rt_strlen(data));
  91. log_debug("set wristband device physique status(%s).", status->Physique);
  92. break;
  93. default:
  94. log_error("input cmd(%d) error\n", cmd);
  95. break;
  96. }
  97. return 0;
  98. }
  99. int joylink_band_set(int argc, char **argv)
  100. {
  101. if (argc != 3)
  102. {
  103. rt_kprintf("joylink_band_set <sport/sleep/physique> <value> -- set joylink wristband wristband device information\n");
  104. return -1;
  105. }
  106. if (rt_strcmp(argv[1], "sport") == 0)
  107. {
  108. joylink_dev_user_data_set(USER_DATA_CMD_SPORT, (void *)argv[2]);
  109. }
  110. else if (rt_strcmp(argv[1], "sleep") == 0)
  111. {
  112. joylink_dev_user_data_set(USER_DATA_CMD_SLEEP, (void *)argv[2]);
  113. }
  114. else if (rt_strcmp(argv[1], "physique") == 0)
  115. {
  116. joylink_dev_user_data_set(USER_DATA_CMD_PHYSIQUE, (void *)argv[2]);
  117. }
  118. else
  119. {
  120. rt_kprintf("joylink_band_set <sport/sleep/physique> <value> -- set joylink wristband device information\n");
  121. return -1;
  122. }
  123. return 0;
  124. }
  125. #ifdef FINSH_USING_MSH
  126. #include <finsh.h>
  127. MSH_CMD_EXPORT(joylink_band_set, set joylink wristband device information);
  128. #endif /* FINSH_USING_MSH */
  129. /* prase the json device information */
  130. static int joylink_dev_parse_ctrl(const char *pMsg, user_dev_status_t *userDev)
  131. {
  132. int ret = -1;
  133. cJSON *pSub, *pJson, *pStreams;
  134. if (NULL == pMsg || NULL == userDev)
  135. {
  136. goto RET;
  137. }
  138. log_debug("json_org:%s", pMsg);
  139. pJson = cJSON_Parse(pMsg);
  140. if (RT_NULL == pJson)
  141. {
  142. log_error("--->:ERROR: pMsg is NULL\n");
  143. goto RET;
  144. }
  145. pStreams = cJSON_GetObjectItem(pJson, "streams");
  146. if (RT_NULL != pStreams)
  147. {
  148. int iSize = cJSON_GetArraySize(pStreams);
  149. int iCnt;
  150. for( iCnt = 0; iCnt < iSize; iCnt++)
  151. {
  152. char *dout = RT_NULL;
  153. pSub = cJSON_GetArrayItem(pStreams, iCnt);
  154. if (NULL == pSub)
  155. continue;
  156. cJSON *pSId = cJSON_GetObjectItem(pSub, "stream_id");
  157. if (NULL == pSId)
  158. break;
  159. cJSON *pV = cJSON_GetObjectItem(pSub, "current_value");
  160. if (NULL == pV)
  161. continue;
  162. if (!strcmp(USER_DATA_SPORT, pSId->valuestring))
  163. {
  164. memset(userDev->Sport, 0, sizeof(userDev->Sport));
  165. strcpy(userDev->Sport, pV->valuestring);
  166. }
  167. if (!strcmp(USER_DATA_SLEEP, pSId->valuestring))
  168. {
  169. memset(userDev->Sleep, 0, sizeof(userDev->Sleep));
  170. strcpy(userDev->Sleep, pV->valuestring);
  171. }
  172. if (!strcmp(USER_DATA_PHYSIQUE, pSId->valuestring))
  173. {
  174. memset(userDev->Physique, 0, sizeof(userDev->Physique));
  175. strcpy(userDev->Physique, pV->valuestring);
  176. }
  177. dout = cJSON_Print(pSub);
  178. if (RT_NULL != dout)
  179. {
  180. log_debug("org streams:%s", dout);
  181. rt_free(dout);
  182. }
  183. }
  184. }
  185. cJSON_Delete(pJson);
  186. RET:
  187. return ret;
  188. }
  189. /* Generate json format data by user device information */
  190. static char *joylink_dev_package_info(const int retCode, user_dev_status_t *userDev)
  191. {
  192. cJSON *root, *arrary;
  193. cJSON *data_Sport, *data_Sleep, *data_Physique;
  194. char *out;
  195. if (RT_NULL == userDev)
  196. {
  197. return RT_NULL;
  198. }
  199. root = cJSON_CreateObject();
  200. if (RT_NULL == root)
  201. {
  202. goto RET;
  203. }
  204. arrary = cJSON_CreateArray();
  205. if (RT_NULL == arrary)
  206. {
  207. cJSON_Delete(root);
  208. goto RET;
  209. }
  210. cJSON_AddNumberToObject(root, "code", retCode);
  211. cJSON_AddItemToObject(root, "streams", arrary);
  212. data_Sport = cJSON_CreateObject();
  213. cJSON_AddItemToArray(arrary, data_Sport);
  214. cJSON_AddStringToObject(data_Sport, "stream_id", "Sport");
  215. cJSON_AddStringToObject(data_Sport, "current_value", userDev->Sport);
  216. data_Sleep = cJSON_CreateObject();
  217. cJSON_AddItemToArray(arrary, data_Sleep);
  218. cJSON_AddStringToObject(data_Sleep, "stream_id", "Sleep");
  219. cJSON_AddStringToObject(data_Sleep, "current_value", userDev->Sleep);
  220. data_Physique = cJSON_CreateObject();
  221. cJSON_AddItemToArray(arrary, data_Physique);
  222. cJSON_AddStringToObject(data_Physique, "stream_id", "Physique");
  223. cJSON_AddStringToObject(data_Physique, "current_value", userDev->Physique);
  224. out = cJSON_Print(root);
  225. cJSON_Delete(root);
  226. RET:
  227. return out;
  228. }
  229. /* Get device information */
  230. static char * joylink_dev_modelcode_info(const int retCode, user_dev_status_t *userDev)
  231. {
  232. cJSON *root, *arrary, *element;
  233. char *out = RT_NULL;
  234. char i2str[32];
  235. if (RT_NULL == userDev)
  236. {
  237. return RT_NULL;
  238. }
  239. root = cJSON_CreateObject();
  240. if (RT_NULL == root)
  241. {
  242. goto RET;
  243. }
  244. arrary = cJSON_CreateArray();
  245. if (RT_NULL == arrary)
  246. {
  247. cJSON_Delete(root);
  248. goto RET;
  249. }
  250. cJSON_AddItemToObject(root, "model_codes", arrary);
  251. rt_memset(i2str, 0, sizeof(i2str));
  252. element = cJSON_CreateObject();
  253. cJSON_AddItemToArray(arrary, element);
  254. cJSON_AddStringToObject(element, "feedid", "247828880060773075");
  255. cJSON_AddStringToObject(element, "model_code", "12345678123456781234567812345678");
  256. out = cJSON_Print(root);
  257. cJSON_Delete(root);
  258. RET:
  259. return out;
  260. }
  261. /* device get model_code information data for the JSON format */
  262. int joylink_dev_get_modelcode(char *out_modelcode, int32_t out_max)
  263. {
  264. int len = 0;
  265. char *packet_data = RT_NULL;
  266. if (RT_NULL == out_modelcode || out_max < 0)
  267. {
  268. return 0;
  269. }
  270. packet_data = joylink_dev_modelcode_info(0, &user_dev);
  271. if (RT_NULL != packet_data)
  272. {
  273. len = rt_strlen(packet_data);
  274. log_debug("------>%s:len:%d\n", packet_data, len);
  275. if (len < out_max)
  276. {
  277. rt_memcpy(out_modelcode, packet_data, len);
  278. }
  279. }
  280. if (RT_NULL != packet_data)
  281. {
  282. rt_free(packet_data);
  283. }
  284. return len < out_max ? len : 0;
  285. }
  286. /* device get current device information data for the JSON foramt */
  287. int joylink_dev_get_snap_shot_with_retcode(int32_t ret_code, char *out_snap, int32_t out_max)
  288. {
  289. int len = 0;
  290. char *packet_data = RT_NULL;
  291. if (RT_NULL == out_snap || out_max < 0)
  292. {
  293. return 0;
  294. }
  295. packet_data = joylink_dev_package_info(ret_code, &user_dev);
  296. if (RT_NULL != packet_data)
  297. {
  298. len = rt_strlen(packet_data);
  299. log_debug("------>%s:len:%d\n", packet_data, len);
  300. if (len < out_max)
  301. {
  302. rt_memcpy(out_snap, packet_data, len);
  303. }
  304. }
  305. if (RT_NULL != packet_data)
  306. {
  307. rt_free(packet_data);
  308. }
  309. return len < out_max ? len : 0;
  310. }
  311. E_JLRetCode_t joylink_dev_script_ctrl(const char *src, int src_len, JLContrl_t *ctr, int from_server)
  312. {
  313. E_JLRetCode_t ret = E_RET_ERROR;
  314. time_t tt;
  315. if (RT_NULL == src || RT_NULL == ctr)
  316. {
  317. return E_RET_ERROR;
  318. }
  319. ctr->biz_code = (int)(*((int *)(src + 4)));
  320. ctr->serial = (int)(*((int *)(src +8)));
  321. tt = time(NULL);
  322. log_info("bcode:%d:server:%d:time:%ld", ctr->biz_code, from_server, (long)tt);
  323. if (ctr->biz_code == JL_BZCODE_GET_SNAPSHOT)
  324. {
  325. ret = E_RET_OK;
  326. }
  327. else if (ctr->biz_code == JL_BZCODE_CTRL)
  328. {
  329. joylink_dev_parse_ctrl(src + 12, &user_dev);
  330. return E_RET_OK;
  331. }
  332. else
  333. {
  334. log_error("unKown biz_code:%d", ctr->biz_code);
  335. }
  336. return ret;
  337. }
  338. #ifdef JOYLINK_USING_AUTO_INIT
  339. INIT_APP_EXPORT(joylink_start);
  340. #endif
  341. #ifdef FINSH_USING_MSH
  342. #include <finsh.h>
  343. MSH_CMD_EXPORT(joylink_start, joylink cloud work start);
  344. MSH_CMD_EXPORT(joylink_stop, joylink cloud work stop);
  345. MSH_CMD_EXPORT(joylink_config_reset, joylink wifi configuration reset);
  346. MSH_CMD_EXPORT(joylink_mode_change, joylink network configuration mode change);
  347. #endif /* FINSH_USING_MSH */