cisAsynEntry.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. /****************************************************************************
  2. *
  3. * Copy right: 2017-, Copyrigths of EigenComm Ltd.
  4. * File name: cisAsynEntry.c
  5. * Description: EC616 onenet cis async entry source file
  6. * History: Rev1.0 2018-10-12
  7. *
  8. ****************************************************************************/
  9. #include <cis_if_sys.h>
  10. #include <cis_list.h>
  11. #include "cisAsynEntry.h"
  12. #include "osasys.h"
  13. #include "task.h"
  14. #if defined CHIP_EC616 || defined CHIP_EC616_Z0
  15. #include "slpman_ec616.h"
  16. #include "Flash_ec616_rt.h"
  17. #elif defined CHIP_EC617
  18. #include "slpman_ec617.h"
  19. #include "Flash_ec617_rt.h"
  20. #endif
  21. #include "lfs_port.h"
  22. #include "debug_log.h"
  23. //#define DEBUG_SLEEP_BACKUP
  24. #define USE_PLAIN_BOOTSTRAP (0)
  25. #define ONENET_TASK_STACK_SIZE (1024*3)
  26. #define CIS_ENABLE_CONTEXT_RESTORE (1)
  27. #if 0
  28. //authcode:EC616 psk:EIGENCOMM 183.230.40.39:5684
  29. static const uint8_t config_hex[] = {
  30. 0x13, 0x00, 0x53,
  31. 0xf1, 0x00, 0x03,
  32. 0xf2, 0x00, 0x45,
  33. 0x05, 0x00 /*mtu*/, 0x11 /*Link & bind type*/, 0xC0 /*BS DTLS ENABLED*/,
  34. 0x00, 0x05 /*apn length*/, 0x43, 0x4d, 0x49, 0x4f, 0x54 /*apn: CMIOT*/,
  35. 0x00, 0x00 /*username length*/, /*username*/
  36. 0x00, 0x00 /*password length*/, /*password*/
  37. 0x00, 0x12 /*host length*/, 0x31, 0x38, 0x33, 0x2e, 0x32, 0x33, 0x30, 0x2e,
  38. 0x34, 0x30, 0x2e, 0x33, 0x39, 0x3a, 0x35, 0x36, 0x38, 0x34 /*host: 183.230.40.39:5684*/,
  39. 0x00, 0x1D /*userdata length*/,
  40. 0x41, 0x75, 0x74, 0x68, 0x43, 0x6f, 0x64, 0x65,
  41. 0x3a, 0x45, 0x43, 0x36, 0x31, 0x36, 0x3b, 0x50,
  42. 0x53, 0x4b, 0x3a, 0x45, 0x49, 0x47, 0x45, 0x4E,
  43. 0x43, 0x4F, 0x4D, 0x4D,0x3b /*userdata: AuthCode:EC616;PSK:EIGENCOMM;*/,
  44. 0xf3, 0x00, 0x08,0xe4 /*log config*/, 0x00, 0xc8 /*LogBufferSize: 200*/,
  45. 0x00, 0x00 /*userdata length*//*userdata*/
  46. };
  47. //authcode:EC616 psk:EIGENCOMM 183.230.40.40:5683
  48. static const uint8_t config_hex[] = {
  49. 0x13, 0x00, 0x53,
  50. 0xf1, 0x00, 0x03,
  51. 0xf2, 0x00, 0x45,
  52. 0x05, 0x00 /*mtu*/, 0x11 /*Link & bind type*/, 0x00 /*BS DTLS DISABLED*/,
  53. 0x00, 0x05 /*apn length*/, 0x43, 0x4d, 0x49, 0x4f, 0x54 /*apn: CMIOT*/,
  54. 0x00, 0x00 /*username length*/, /*username*/
  55. 0x00, 0x00 /*password length*/, /*password*/
  56. 0x00, 0x12 /*host length*/,
  57. 0x31, 0x38, 0x33, 0x2e, 0x32, 0x33, 0x30, 0x2e,
  58. 0x34, 0x30, 0x2e, 0x34, 0x30, 0x3a, 0x35, 0x36, 0x38, 0x33 /*host: 183.230.40.40:5683*/,
  59. 0x00, 0x1D /*userdata length*/, 0x41, 0x75, 0x74, 0x68, 0x43, 0x6f, 0x64, 0x65,
  60. 0x3a, 0x45, 0x43, 0x36, 0x31, 0x36, 0x3b, 0x50,
  61. 0x53, 0x4b, 0x3a, 0x45, 0x49, 0x47, 0x45, 0x4E,
  62. 0x43, 0x4F, 0x4D, 0x4D,0x3b /*userdata: AuthCode:EC616;PSK:EIGENCOMM;*/,
  63. 0xf3, 0x00, 0x08,0xe4 /*log config*/, 0x00, 0xc8 /*LogBufferSize: 200*/,
  64. 0x00, 0x00 /*userdata length*//*userdata*/
  65. };
  66. #endif
  67. //authcode: psk: 183.230.40.39:5683 enable bootstrap
  68. static const uint8_t config_hex[] = {
  69. 0x13, 0x00, 0x45,
  70. 0xf1, 0x00, 0x03,
  71. 0xf2, 0x00, 0x37,
  72. 0x05, 0x00 /*mtu*/, 0x11 /*Link & bind type*/, 0x80 /*BS ENABLE DTLS DISABLED*/,
  73. 0x00, 0x05 /*apn length*/, 0x43, 0x4d, 0x49, 0x4f, 0x54 /*apn: CMIOT*/,
  74. 0x00, 0x00 /*username length*/, /*username*/
  75. 0x00, 0x00 /*password length*/, /*password*/
  76. 0x00, 0x12 /*host length*/,
  77. 0x31, 0x38, 0x33, 0x2e, 0x32, 0x33, 0x30, 0x2e,
  78. 0x34, 0x30, 0x2e, 0x33, 0x39, 0x3a, 0x35, 0x36, 0x38, 0x33 /*host: 183.230.40.40:5683*/,
  79. 0x00, 0x0f /*userdata length*/, 0x41, 0x75, 0x74, 0x68, 0x43, 0x6f, 0x64, 0x65,
  80. 0x3a, 0x3b, 0x50, 0x53, 0x4b, 0x3a, 0x3b/*userdata: AuthCode:;PSK:;*/,
  81. 0xf3, 0x00, 0x08,0xe4 /*log config*/, 0x00, 0xc8 /*LogBufferSize: 200*/,
  82. 0x00, 0x00 /*userdata length*//*userdata*/
  83. };
  84. static StaticTask_t onenetTask;
  85. static osThreadId_t onenetTaskId = NULL;
  86. static uint8_t onenetTaskStack[ONENET_TASK_STACK_SIZE];
  87. static void* gCisContext = NULL;
  88. static bool g_shutdown = false;
  89. static cis_time_t g_lifetimeLast = 0;
  90. static cis_time_t g_lifetime = 0;
  91. static cis_time_t g_notifyLast = 0;
  92. static bool gNotifyOngoing = false;
  93. static bool gNotifyOnceSuccess = false;
  94. static struct st_callback_info* g_callbackList = NULL;
  95. static struct st_observe_info* g_observeList = NULL;
  96. static st_sample_object g_objectList[SAMPLE_OBJECT_MAX];
  97. static st_instance_a g_instList_a[SAMPLE_A_INSTANCE_COUNT];
  98. static CHAR *defaultLocalPort = "40962";
  99. static onenet_context_t gOnenetContextRunning;
  100. extern observed_backup_t g_observed_backup[MAX_OBSERVED_COUNT];
  101. //////////////////////////////////////////////////////////////////////////
  102. //private funcation;
  103. static void prvObserveNotify(void* context,cis_uri_t* uri,cis_mid_t mid)
  104. {
  105. uint8_t index;
  106. st_sample_object* object = NULL;
  107. cis_data_t value;
  108. for (index = 0;index < SAMPLE_OBJECT_MAX;index++)
  109. {
  110. if(g_objectList[index].oid == uri->objectId){
  111. object = &g_objectList[index];
  112. }
  113. }
  114. if(object == NULL){
  115. ECOMM_TRACE(UNILOG_PLA_APP, x1_prv_observeNotify_0, P_ERROR, 0, "prv_observeNotify return");
  116. return;
  117. }
  118. ECOMM_TRACE(UNILOG_PLA_APP, x1_prv_observeNotify_1, P_INFO, 0, "prv_observeNotify called");
  119. if(!CIS_URI_IS_SET_INSTANCE(uri) && !CIS_URI_IS_SET_RESOURCE(uri))
  120. {
  121. switch(uri->objectId)
  122. {
  123. case SAMPLE_OID_A:
  124. {
  125. for(index=0;index<SAMPLE_A_INSTANCE_COUNT;index++)
  126. {
  127. st_instance_a *inst = &g_instList_a[index];
  128. if(inst != NULL && inst->enabled == true)
  129. {
  130. cis_data_t tmpdata[4];
  131. tmpdata[0].type = cis_data_type_integer;
  132. tmpdata[0].value.asInteger = inst->instance.intValue;
  133. uri->instanceId = inst->instId;
  134. uri->resourceId = attributeA_intValue;
  135. cis_uri_update(uri);
  136. cis_notify_ec(context,uri,&tmpdata[0],mid,CIS_NOTIFY_CONTINUE,true, PS_SOCK_RAI_NO_INFO);
  137. tmpdata[2].type = cis_data_type_bool;
  138. tmpdata[2].value.asBoolean = inst->instance.boolValue;
  139. uri->resourceId = attributeA_boolValue;
  140. uri->instanceId = inst->instId;
  141. cis_uri_update(uri);
  142. cis_notify_ec(context,uri,&tmpdata[2],mid,CIS_NOTIFY_CONTINUE,true, PS_SOCK_RAI_NO_INFO);
  143. tmpdata[3].type = cis_data_type_string;
  144. tmpdata[3].asBuffer.length = strlen(inst->instance.strValue);
  145. tmpdata[3].asBuffer.buffer = (uint8_t*)(inst->instance.strValue);
  146. uri->resourceId = attributeA_stringValue;
  147. uri->instanceId = inst->instId;
  148. cis_uri_update(uri);
  149. cis_notify_ec(context,uri,&tmpdata[3],mid,CIS_NOTIFY_CONTENT,true, PS_SOCK_RAI_NO_INFO);
  150. }
  151. }
  152. }
  153. break;
  154. }
  155. }else if(CIS_URI_IS_SET_INSTANCE(uri))
  156. {
  157. switch(object->oid)
  158. {
  159. case SAMPLE_OID_A:
  160. {
  161. if(uri->instanceId > SAMPLE_A_INSTANCE_COUNT){
  162. return;
  163. }
  164. st_instance_a *inst = &g_instList_a[uri->instanceId];
  165. if(inst == NULL || inst->enabled == false){
  166. return;
  167. }
  168. if(CIS_URI_IS_SET_RESOURCE(uri)){
  169. if(uri->resourceId == attributeA_intValue)
  170. {
  171. value.type = cis_data_type_integer;
  172. value.value.asInteger = inst->instance.intValue;
  173. }
  174. else if(uri->resourceId == attributeA_boolValue)
  175. {
  176. value.type = cis_data_type_bool;
  177. value.value.asBoolean = inst->instance.boolValue;
  178. }
  179. else if(uri->resourceId == attributeA_stringValue)
  180. {
  181. value.type = cis_data_type_string;
  182. value.asBuffer.length = strlen(inst->instance.strValue);
  183. value.asBuffer.buffer = (uint8_t*)(inst->instance.strValue);
  184. }else{
  185. return;
  186. }
  187. cis_notify_ec(context,uri,&value,mid,CIS_NOTIFY_CONTENT,true, PS_SOCK_RAI_NO_INFO);
  188. }else{
  189. cis_data_t tmpdata[4];
  190. tmpdata[0].type = cis_data_type_integer;
  191. tmpdata[0].value.asInteger = inst->instance.intValue;
  192. uri->resourceId = attributeA_intValue;
  193. cis_uri_update(uri);
  194. cis_notify_ec(context,uri,&tmpdata[0],mid,CIS_NOTIFY_CONTINUE,true, PS_SOCK_RAI_NO_INFO);
  195. tmpdata[2].type = cis_data_type_bool;
  196. tmpdata[2].value.asBoolean = inst->instance.boolValue;
  197. uri->resourceId = attributeA_boolValue;
  198. cis_uri_update(uri);
  199. cis_notify_ec(context,uri,&tmpdata[2],mid,CIS_NOTIFY_CONTINUE,true, PS_SOCK_RAI_NO_INFO);
  200. tmpdata[3].type = cis_data_type_string;
  201. tmpdata[3].asBuffer.length = strlen(inst->instance.strValue);
  202. tmpdata[3].asBuffer.buffer = (uint8_t*)(inst->instance.strValue);
  203. uri->resourceId = attributeA_stringValue;
  204. cis_uri_update(uri);
  205. cis_notify_ec(context,uri,&tmpdata[3],mid,CIS_NOTIFY_CONTENT,true, PS_SOCK_RAI_NO_INFO);
  206. }
  207. }
  208. break;
  209. }
  210. }
  211. }
  212. static void prvReadResponse(void* context,cis_uri_t* uri,cis_mid_t mid)
  213. {
  214. uint8_t index;
  215. st_sample_object* object = NULL;
  216. cis_data_t value;
  217. for (index = 0;index < SAMPLE_OBJECT_MAX;index++)
  218. {
  219. if(g_objectList[index].oid == uri->objectId){
  220. object = &g_objectList[index];
  221. }
  222. }
  223. if(object == NULL){
  224. return;
  225. }
  226. if(!CIS_URI_IS_SET_INSTANCE(uri) && !CIS_URI_IS_SET_RESOURCE(uri)) // one object
  227. {
  228. switch(uri->objectId)
  229. {
  230. case SAMPLE_OID_A:
  231. {
  232. for(index=0;index<SAMPLE_A_INSTANCE_COUNT;index++)
  233. {
  234. st_instance_a *inst = &g_instList_a[index];
  235. if(inst != NULL && inst->enabled == true)
  236. {
  237. cis_data_t tmpdata[4];
  238. tmpdata[0].type = cis_data_type_integer;
  239. tmpdata[0].value.asInteger = inst->instance.intValue;
  240. uri->instanceId = inst->instId;
  241. uri->resourceId = attributeA_intValue;
  242. cis_uri_update(uri);
  243. cis_response(context,uri,&tmpdata[0],mid,CIS_RESPONSE_CONTINUE, PS_SOCK_RAI_NO_INFO);
  244. tmpdata[2].type = cis_data_type_bool;
  245. tmpdata[2].value.asBoolean = inst->instance.boolValue;
  246. uri->resourceId = attributeA_boolValue;
  247. uri->instanceId = inst->instId;
  248. cis_uri_update(uri);
  249. cis_response(context,uri,&tmpdata[2],mid,CIS_RESPONSE_CONTINUE, PS_SOCK_RAI_NO_INFO);
  250. tmpdata[3].type = cis_data_type_string;
  251. tmpdata[3].asBuffer.length = strlen(inst->instance.strValue);
  252. tmpdata[3].asBuffer.buffer = (uint8_t*)strdup(inst->instance.strValue);
  253. uri->resourceId = attributeA_stringValue;
  254. uri->instanceId = inst->instId;
  255. cis_uri_update(uri);
  256. cis_response(context,uri,&tmpdata[3],mid,CIS_RESPONSE_CONTINUE, PS_SOCK_RAI_NO_INFO);
  257. }
  258. }
  259. }
  260. break;
  261. }
  262. cis_response(context,NULL,NULL,mid,CIS_RESPONSE_READ, PS_SOCK_RAI_NO_INFO);
  263. }else
  264. {
  265. switch(object->oid)
  266. {
  267. case SAMPLE_OID_A:
  268. {
  269. if(uri->instanceId > SAMPLE_A_INSTANCE_COUNT){
  270. return;
  271. }
  272. st_instance_a *inst = &g_instList_a[uri->instanceId];
  273. if(inst == NULL || inst->enabled == false){
  274. return;
  275. }
  276. if(CIS_URI_IS_SET_RESOURCE(uri)){
  277. if(uri->resourceId == attributeA_intValue)
  278. {
  279. value.type = cis_data_type_integer;
  280. value.value.asInteger = inst->instance.intValue;
  281. }
  282. else if(uri->resourceId == attributeA_boolValue)
  283. {
  284. value.type = cis_data_type_bool;
  285. value.value.asBoolean = inst->instance.boolValue;
  286. }
  287. else if(uri->resourceId == attributeA_stringValue)
  288. {
  289. value.type = cis_data_type_string;
  290. value.asBuffer.length = strlen(inst->instance.strValue);
  291. value.asBuffer.buffer = (uint8_t*)strdup(inst->instance.strValue);
  292. }else{
  293. return;
  294. }
  295. cis_response(context,uri,&value,mid,CIS_RESPONSE_READ, PS_SOCK_RAI_NO_INFO);
  296. }else{
  297. cis_data_t tmpdata[4];
  298. tmpdata[0].type = cis_data_type_integer;
  299. tmpdata[0].value.asInteger = inst->instance.intValue;
  300. uri->resourceId = attributeA_intValue;
  301. cis_uri_update(uri);
  302. cis_response(context,uri,&tmpdata[0],mid,CIS_RESPONSE_CONTINUE, PS_SOCK_RAI_NO_INFO);
  303. tmpdata[2].type = cis_data_type_bool;
  304. tmpdata[2].value.asBoolean = inst->instance.boolValue;
  305. uri->resourceId = attributeA_boolValue;
  306. cis_uri_update(uri);
  307. cis_response(context,uri,&tmpdata[2],mid,CIS_RESPONSE_CONTINUE, PS_SOCK_RAI_NO_INFO);
  308. tmpdata[3].type = cis_data_type_string;
  309. tmpdata[3].asBuffer.length = strlen(inst->instance.strValue);
  310. tmpdata[3].asBuffer.buffer = (uint8_t*)strdup(inst->instance.strValue);
  311. uri->resourceId = attributeA_stringValue;
  312. cis_uri_update(uri);
  313. cis_response(context,uri,&tmpdata[3],mid,CIS_RESPONSE_READ, PS_SOCK_RAI_NO_INFO);
  314. }
  315. }
  316. break;
  317. }
  318. }
  319. }
  320. static void prvDiscoverResponse(void* context,cis_uri_t* uri,cis_mid_t mid)
  321. {
  322. uint8_t index;
  323. st_sample_object* object = NULL;
  324. for (index = 0;index < SAMPLE_OBJECT_MAX;index++)
  325. {
  326. if(g_objectList[index].oid == uri->objectId){
  327. object = &g_objectList[index];
  328. }
  329. }
  330. if(object == NULL){
  331. return;
  332. }
  333. switch(uri->objectId)
  334. {
  335. case SAMPLE_OID_A:
  336. {
  337. uri->objectId = SAMPLE_OID_A;
  338. uri->instanceId = 0;
  339. uri->resourceId = attributeA_intValue;
  340. cis_uri_update(uri);
  341. cis_response(context,uri,NULL,mid,CIS_RESPONSE_CONTINUE, PS_SOCK_RAI_NO_INFO);
  342. uri->objectId = SAMPLE_OID_A;
  343. uri->instanceId = 0;
  344. uri->resourceId = attributeA_boolValue;
  345. cis_uri_update(uri);
  346. cis_response(context,uri,NULL,mid,CIS_RESPONSE_CONTINUE, PS_SOCK_RAI_NO_INFO);
  347. uri->objectId = SAMPLE_OID_A;
  348. uri->instanceId = 0;
  349. uri->resourceId = attributeA_stringValue;
  350. cis_uri_update(uri);
  351. cis_response(context,uri,NULL,mid,CIS_RESPONSE_CONTINUE, PS_SOCK_RAI_NO_INFO);
  352. }
  353. break;
  354. }
  355. cis_response(context,NULL,NULL,mid,CIS_RESPONSE_DISCOVER, PS_SOCK_RAI_NO_INFO);
  356. }
  357. static void prvWriteResponse(void* context,cis_uri_t* uri,const cis_data_t* value,cis_attrcount_t count,cis_mid_t mid)
  358. {
  359. uint8_t index;
  360. st_sample_object* object = NULL;
  361. if(!CIS_URI_IS_SET_INSTANCE(uri))
  362. {
  363. return;
  364. }
  365. for (index = 0;index < SAMPLE_OBJECT_MAX;index++)
  366. {
  367. if(g_objectList[index].oid == uri->objectId){
  368. object = &g_objectList[index];
  369. }
  370. }
  371. if(object == NULL){
  372. return;
  373. }
  374. switch(object->oid)
  375. {
  376. case SAMPLE_OID_A:
  377. {
  378. if(uri->instanceId > SAMPLE_A_INSTANCE_COUNT){
  379. return;
  380. }
  381. st_instance_a *inst = &g_instList_a[uri->instanceId];
  382. if(inst == NULL || inst->enabled == false){
  383. return;
  384. }
  385. for (int i=0;i<count;i++)
  386. {
  387. ECOMM_TRACE(UNILOG_PLA_APP, prvWriteResponse_1, P_INFO, 3, "prvWriteResponse:write %d/%d/%d", uri->objectId,uri->instanceId,value[i].id);
  388. switch(value[i].id)
  389. {
  390. case attributeA_intValue:
  391. {
  392. if(value[i].type == cis_data_type_string){
  393. inst->instance.intValue = atoi((const char*)value[i].asBuffer.buffer);
  394. }else{
  395. inst->instance.intValue = value[i].value.asInteger;
  396. }
  397. }
  398. break;
  399. case attributeA_boolValue:
  400. {
  401. if(value[i].type == cis_data_type_string){
  402. inst->instance.boolValue = atoi((const char*)value[i].asBuffer.buffer);
  403. }else{
  404. inst->instance.boolValue = value[i].value.asBoolean;
  405. }
  406. }
  407. break;
  408. case attributeA_stringValue:
  409. {
  410. memset(inst->instance.strValue,0,sizeof(inst->instance.strValue));
  411. strncpy(inst->instance.strValue,(char*)value[i].asBuffer.buffer,value[i].asBuffer.length);
  412. }
  413. break;
  414. }
  415. }
  416. }
  417. break;
  418. }
  419. cis_response(context,NULL,NULL,mid,CIS_RESPONSE_WRITE, PS_SOCK_RAI_NO_INFO);
  420. }
  421. static void prvExecResponse(void* context,cis_uri_t* uri,const uint8_t* value,uint32_t length,cis_mid_t mid)
  422. {
  423. uint8_t index;
  424. st_sample_object* object = NULL;
  425. for (index = 0;index < SAMPLE_OBJECT_MAX;index++)
  426. {
  427. if(g_objectList[index].oid == uri->objectId){
  428. object = &g_objectList[index];
  429. }
  430. }
  431. if(object == NULL){
  432. return;
  433. }
  434. switch(object->oid)
  435. {
  436. case SAMPLE_OID_A:
  437. {
  438. if(uri->instanceId > SAMPLE_A_INSTANCE_COUNT){
  439. return;
  440. }
  441. st_instance_a *inst = &g_instList_a[uri->instanceId];
  442. if(inst == NULL || inst->enabled == false){
  443. return;
  444. }
  445. if(uri->resourceId == actionA_1)
  446. {
  447. /*
  448. *\call action;
  449. */
  450. ECOMM_TRACE(UNILOG_PLA_APP, prvExecResponse_1, P_INFO, 0, "exec actionA_action");
  451. cis_response(context,NULL,NULL,mid,CIS_RESPONSE_EXECUTE, PS_SOCK_RAI_NO_INFO);
  452. }else{
  453. return;
  454. }
  455. }
  456. break;
  457. }
  458. };
  459. static void prvParamsResponse (void* context, cis_uri_t* uri, cis_observe_attr_t parameters, cis_mid_t mid)
  460. {
  461. uint8_t index;
  462. st_sample_object* object = NULL;
  463. if(CIS_URI_IS_SET_RESOURCE(uri)){
  464. ECOMM_TRACE(UNILOG_PLA_APP, prvParamsResponse_1, P_INFO, 3, "prvParamsResponse (%d/%d/%d)", uri->objectId,uri->instanceId,uri->resourceId);
  465. }
  466. if(!CIS_URI_IS_SET_INSTANCE(uri))
  467. {
  468. return;
  469. }
  470. for (index = 0;index < SAMPLE_OBJECT_MAX;index++)
  471. {
  472. if(g_objectList[index].oid == uri->objectId){
  473. object = &g_objectList[index];
  474. }
  475. }
  476. if(object == NULL){
  477. return;
  478. }
  479. /*set parameter to observe resource*/
  480. ECOMM_TRACE(UNILOG_PLA_APP, prvParamsResponse_2, P_INFO, 2, "prvParamsResponse set:%x,clr:%x", parameters.toSet, parameters.toClear);
  481. ECOMM_TRACE(UNILOG_PLA_APP, prvParamsResponse_3, P_INFO, 5, "prvParamsResponse min:%d,max:%d,gt:%f,lt:%f,st:%f", parameters.minPeriod,parameters.maxPeriod,parameters.greaterThan,parameters.lessThan,parameters.step);
  482. cis_response(context,NULL,NULL,mid,CIS_RESPONSE_OBSERVE_PARAMS, PS_SOCK_RAI_NO_INFO);
  483. }
  484. static cis_data_t* prvDataDup(const cis_data_t* value,cis_attrcount_t attrcount)
  485. {
  486. cis_attrcount_t index;
  487. cis_data_t* newData;
  488. newData =(cis_data_t*)cissys_malloc(attrcount * sizeof(cis_data_t));
  489. if(newData == NULL)
  490. {
  491. return NULL;
  492. }
  493. for (index =0;index < attrcount;index++)
  494. {
  495. newData[index].id = value[index].id;
  496. newData[index].type = value[index].type;
  497. newData[index].asBuffer.length = value[index].asBuffer.length;
  498. newData[index].asBuffer.buffer = (uint8_t*)cissys_malloc(value[index].asBuffer.length);
  499. memcpy(newData[index].asBuffer.buffer,value[index].asBuffer.buffer,value[index].asBuffer.length);
  500. memcpy(&newData[index].value.asInteger,&value[index].value.asInteger,sizeof(newData[index].value));
  501. }
  502. return newData;
  503. }
  504. static void prvUpdateObserveContext()
  505. {
  506. struct st_observe_info* node = g_observeList;
  507. uint8_t i = 0;
  508. while(node != NULL)
  509. {
  510. ECOMM_TRACE(UNILOG_PLA_APP, x1_prvUpdateObserveContext_0, P_INFO, 1, "prvUpdateObserveContext mid=%d", node->mid);
  511. node = node->next;
  512. i++;
  513. }
  514. gOnenetContextRunning.observeObjNum = i;
  515. #ifdef DEBUG_SLEEP_BACKUP
  516. printf("prvUpdateObserveContext:");
  517. for(int j= 0; j < sizeof(observed_backup_t); j ++)
  518. printf("%d ", ((UINT8*)&(g_observed_backup[0]))[j]);
  519. printf("\n");
  520. #endif
  521. memcpy((uint8_t *)&(gOnenetContextRunning.gObservedBackup[0]), &(g_observed_backup[0]), MAX_OBSERVED_COUNT*sizeof(observed_backup_t));
  522. if(BSP_QSPI_Erase_Safe(APP_BACKUP_NONXIP_ADDR, APP_BACKUP_SIZE) != QSPI_OK) //erase
  523. {
  524. ECOMM_TRACE(UNILOG_PLA_APP, x1_prvUpdateObserveContext_1, P_ERROR, 0, "erase flash error!!!");
  525. }
  526. ECOMM_TRACE(UNILOG_PLA_APP, x1_prvUpdateObserveContext_2, P_INFO, 0, "prvUpdateObserveContext BSP_QSPI_Write_Safe");
  527. if(BSP_QSPI_Write_Safe((uint8_t *)&gOnenetContextRunning, APP_BACKUP_NONXIP_ADDR, sizeof(onenet_context_t))!=QSPI_OK) //programe
  528. {
  529. ECOMM_TRACE(UNILOG_PLA_APP, x1_prvUpdateObserveContext_3, P_ERROR, 0, "write flash error!!!");
  530. }
  531. }
  532. void normal_prvMakeUserdata()
  533. {
  534. int i = 0;
  535. cis_instcount_t instIndex;
  536. cis_instcount_t instCount;
  537. for (i= 0;i < SAMPLE_OBJECT_MAX; i++)
  538. {
  539. st_sample_object* obj = &g_objectList[i];
  540. switch(i){
  541. case 0:
  542. {
  543. obj->oid = SAMPLE_OID_A;
  544. obj->instBitmap = SAMPLE_A_INSTANCE_BITMAP;
  545. instCount = SAMPLE_A_INSTANCE_COUNT;
  546. for (instIndex = 0;instIndex < instCount;instIndex++)
  547. {
  548. if(obj->instBitmap[instIndex] != '1'){
  549. g_instList_a[instIndex].instId = instIndex;
  550. g_instList_a[instIndex].enabled = false;
  551. }
  552. else
  553. {
  554. g_instList_a[instIndex].instId = instIndex;
  555. g_instList_a[instIndex].enabled = true;
  556. g_instList_a[instIndex].instance.boolValue = true;
  557. g_instList_a[instIndex].instance.intValue = cissys_rand() % 100;
  558. strcpy(g_instList_a[instIndex].instance.strValue,"temp test");
  559. }
  560. }
  561. obj->attrCount = sizeof(const_AttrIds_a) / sizeof(cis_rid_t);
  562. obj->attrListPtr = const_AttrIds_a;
  563. obj->actCount = 0;
  564. obj->actListPtr = NULL;
  565. }
  566. break;
  567. }
  568. }
  569. }
  570. //////////////////////////////////////////////////////////////////////////
  571. cis_coapret_t cisAsynOnRead(void* context,cis_uri_t* uri,cis_mid_t mid)
  572. {
  573. struct st_callback_info* newNode = (struct st_callback_info*)cissys_malloc(sizeof(struct st_callback_info));
  574. newNode->next = NULL;
  575. newNode->flag = (et_callback_type_t)CIS_CALLBACK_READ;
  576. newNode->mid = mid;
  577. newNode->uri = *uri;
  578. g_callbackList = (struct st_callback_info*)CIS_LIST_ADD(g_callbackList,newNode);
  579. ECOMM_TRACE(UNILOG_PLA_APP, cisAsynOnRead_1, P_SIG, 3, "cisAsynOnRead (%d/%d/%d)", uri->objectId,uri->instanceId,uri->resourceId);
  580. return CIS_CALLBACK_CONFORM;
  581. }
  582. cis_coapret_t cisAsynOnDiscover(void* context,cis_uri_t* uri,cis_mid_t mid)
  583. {
  584. struct st_callback_info* newNode = (struct st_callback_info*)cissys_malloc(sizeof(struct st_callback_info));
  585. newNode->next = NULL;
  586. newNode->flag = (et_callback_type_t)CIS_CALLBACK_DISCOVER;
  587. newNode->mid = mid;
  588. newNode->uri = *uri;
  589. g_callbackList = (struct st_callback_info*)CIS_LIST_ADD(g_callbackList,newNode);
  590. ECOMM_TRACE(UNILOG_PLA_APP, cisAsynOnDiscover_1, P_SIG, 3, "cisAsynOnDiscover (%d/%d/%d)", uri->objectId,uri->instanceId,uri->resourceId);
  591. return CIS_CALLBACK_CONFORM;
  592. }
  593. cis_coapret_t cisAsynOnWrite(void* context,cis_uri_t* uri,const cis_data_t* value,cis_attrcount_t attrcount,cis_mid_t mid)
  594. {
  595. if(CIS_URI_IS_SET_RESOURCE(uri)){
  596. ECOMM_TRACE(UNILOG_PLA_APP, cisAsynOnWrite_1, P_SIG, 3, "cisAsynOnWrite (%d/%d/%d)", uri->objectId,uri->instanceId,uri->resourceId);
  597. }
  598. else{
  599. ECOMM_TRACE(UNILOG_PLA_APP, cisAsynOnWrite_2, P_SIG, 2, "cisAsynOnWrite (%d/%d)", uri->objectId,uri->instanceId);
  600. }
  601. struct st_callback_info* newNode = (struct st_callback_info*)cissys_malloc(sizeof(struct st_callback_info));
  602. newNode->next = NULL;
  603. newNode->flag = (et_callback_type_t)CIS_CALLBACK_WRITE;
  604. newNode->mid = mid;
  605. newNode->uri = *uri;
  606. newNode->param.asWrite.count = attrcount;
  607. newNode->param.asWrite.value = prvDataDup(value,attrcount);
  608. g_callbackList = (struct st_callback_info*)CIS_LIST_ADD(g_callbackList,newNode);
  609. return CIS_CALLBACK_CONFORM;
  610. }
  611. cis_coapret_t cisAsynOnExec(void* context,cis_uri_t* uri,const uint8_t* value,uint32_t length,cis_mid_t mid)
  612. {
  613. if(CIS_URI_IS_SET_RESOURCE(uri))
  614. {
  615. ECOMM_TRACE(UNILOG_PLA_APP, cisAsynOnExec_1, P_SIG, 3, "cisAsynOnExec (%d/%d/%d)", uri->objectId,uri->instanceId,uri->resourceId);
  616. }
  617. else{
  618. return CIS_CALLBACK_METHOD_NOT_ALLOWED;
  619. }
  620. if(!CIS_URI_IS_SET_INSTANCE(uri))
  621. {
  622. return CIS_CALLBACK_BAD_REQUEST;
  623. }
  624. struct st_callback_info* newNode = (struct st_callback_info*)cissys_malloc(sizeof(struct st_callback_info));
  625. newNode->next = NULL;
  626. newNode->flag = (et_callback_type_t)CIS_CALLBACK_EXECUTE;
  627. newNode->mid = mid;
  628. newNode->uri = *uri;
  629. newNode->param.asExec.buffer = (uint8_t*)cissys_malloc(length);
  630. newNode->param.asExec.length = length;
  631. memcpy(newNode->param.asExec.buffer,value,length);
  632. g_callbackList = (struct st_callback_info*)CIS_LIST_ADD(g_callbackList,newNode);
  633. return CIS_CALLBACK_CONFORM;
  634. }
  635. cis_coapret_t cisAsynOnObserve(void* context,cis_uri_t* uri,bool flag,cis_mid_t mid)
  636. {
  637. ECOMM_TRACE(UNILOG_PLA_APP, cisAsynOnObserve_1, P_SIG, 3, "cisAsynOnObserve mid:%d uri:(%d/%d/%d)",mid,uri->objectId,uri->instanceId,uri->resourceId);
  638. if(!CIS_URI_IS_SET_INSTANCE(uri))
  639. {
  640. return CIS_CALLBACK_BAD_REQUEST;
  641. }
  642. struct st_callback_info* newNode = (struct st_callback_info*)cissys_malloc(sizeof(struct st_callback_info));
  643. newNode->next = NULL;
  644. newNode->flag = (et_callback_type_t)CIS_CALLBACK_OBSERVE;
  645. newNode->mid = mid;
  646. newNode->uri = *uri;
  647. newNode->param.asObserve.flag = flag;
  648. g_callbackList = (struct st_callback_info*)CIS_LIST_ADD(g_callbackList,newNode);
  649. return CIS_CALLBACK_CONFORM;
  650. }
  651. cis_coapret_t cisAsynOnParams(void* context,cis_uri_t* uri,cis_observe_attr_t parameters,cis_mid_t mid)
  652. {
  653. if(CIS_URI_IS_SET_RESOURCE(uri)){
  654. ECOMM_TRACE(UNILOG_PLA_APP, cisAsynOnParams_0, P_SIG, 3, "cisAsynOnParams=(%d/%d/%d)", uri->objectId,uri->instanceId,uri->resourceId);
  655. }
  656. if(!CIS_URI_IS_SET_INSTANCE(uri))
  657. {
  658. return CIS_CALLBACK_BAD_REQUEST;
  659. }
  660. struct st_callback_info* newNode = (struct st_callback_info*)cissys_malloc(sizeof(struct st_callback_info));
  661. newNode->next = NULL;
  662. newNode->flag = (et_callback_type_t)CIS_CALLBACK_SETPARAMS;
  663. newNode->mid = mid;
  664. newNode->uri = *uri;
  665. newNode->param.asObserveParam.params = parameters;
  666. g_callbackList = (struct st_callback_info*)CIS_LIST_ADD(g_callbackList,newNode);
  667. return CIS_CALLBACK_CONFORM;
  668. }
  669. void cisAsynOnEvent(void* context,cis_evt_t eid,void* param)
  670. {
  671. ECOMM_TRACE(UNILOG_PLA_APP, x1_cisAsynOnEvent_0, P_SIG, 1, "cisAsynOnEvent=%d", eid);
  672. switch(eid)
  673. {
  674. case CIS_EVENT_RESPONSE_FAILED:
  675. case CIS_EVENT_NOTIFY_FAILED:
  676. gNotifyOngoing = false;
  677. ECOMM_TRACE(UNILOG_PLA_APP, x1_cisAsynOnEvent_2, P_INFO, 1, "cisAsynOnEvent response failed mid:%d", (int32_t)param);
  678. break;
  679. case CIS_EVENT_NOTIFY_SUCCESS:
  680. gNotifyOngoing = false;
  681. cisAsynSetNotifyOnceStatus(true);
  682. break;
  683. case CIS_EVENT_UPDATE_NEED:
  684. ECOMM_TRACE(UNILOG_PLA_APP, x1_cisAsynOnEvent_3, P_INFO, 1, "cisAsynOnEvent need to update,reserve time:%ds", (int32_t)param);
  685. cis_update_reg(gCisContext,LIFETIME_INVALID,false, PS_SOCK_RAI_NO_INFO);
  686. break;
  687. case CIS_EVENT_REG_SUCCESS:
  688. {
  689. ECOMM_TRACE(UNILOG_PLA_APP, x1_cisAsynOnEvent_4, P_INFO, 1, "cisAsynOnEvent=CIS_EVENT_REG_SUCCESS, will clean g_observeList");
  690. struct st_observe_info* delnode;
  691. while(g_observeList != NULL){
  692. g_observeList =(struct st_observe_info *)CIS_LIST_RM((cis_list_t *)g_observeList,g_observeList->mid,(cis_list_t **)&delnode);
  693. cissys_free(delnode);
  694. }
  695. }
  696. break;
  697. case CIS_EVENT_OBSERVE_ADD:
  698. case CIS_EVENT_OBSERVE_CANCEL:
  699. prvUpdateObserveContext();
  700. break;
  701. default:
  702. break;
  703. }
  704. }
  705. bool cisAsynGetShutdown(void)
  706. {
  707. return g_shutdown;
  708. }
  709. void cisAsynSetShutdown(bool shutdown)
  710. {
  711. g_shutdown = shutdown;
  712. }
  713. bool cisAsynGetNotifyOnceStatus(void)
  714. {
  715. return gNotifyOnceSuccess;
  716. }
  717. void cisAsynSetNotifyOnceStatus(bool success)
  718. {
  719. gNotifyOnceSuccess = success;
  720. }
  721. static void cisAsyncProcessTask(void* lpParam)
  722. {
  723. while(1)
  724. {
  725. struct st_callback_info* node;
  726. if(g_callbackList == NULL || g_shutdown == TRUE)
  727. {
  728. cissys_sleepms(1000);
  729. continue;
  730. }
  731. node = g_callbackList;
  732. g_callbackList = g_callbackList->next;
  733. switch (node->flag)
  734. {
  735. case 0:
  736. break;
  737. case CIS_CALLBACK_READ:
  738. {
  739. ECOMM_TRACE(UNILOG_PLA_APP, cisProcessThread_0, P_SIG, 0, "cisProcessThread CIS_CALLBACK_READ");
  740. cis_uri_t uriLocal;
  741. uriLocal = node->uri;
  742. prvReadResponse(gCisContext,&uriLocal,node->mid);
  743. }
  744. break;
  745. case CIS_CALLBACK_DISCOVER:
  746. {
  747. ECOMM_TRACE(UNILOG_PLA_APP, cisProcessThread_1, P_SIG, 0, "cisProcessThread CIS_CALLBACK_DISCOVER");
  748. cis_uri_t uriLocal;
  749. uriLocal = node->uri;
  750. prvDiscoverResponse(gCisContext,&uriLocal,node->mid);
  751. }
  752. break;
  753. case CIS_CALLBACK_WRITE:
  754. {
  755. ECOMM_TRACE(UNILOG_PLA_APP, cisProcessThread_2, P_SIG, 0, "cisProcessThread CIS_CALLBACK_WRITE");
  756. prvWriteResponse(gCisContext,&node->uri,node->param.asWrite.value,node->param.asWrite.count,node->mid);
  757. cis_data_t* data = node->param.asWrite.value;
  758. cis_attrcount_t count = node->param.asWrite.count;
  759. for (int i=0;i<count;i++)
  760. {
  761. if(data[i].type == cis_data_type_string || data[i].type == cis_data_type_opaque)
  762. {
  763. if(data[i].asBuffer.buffer != NULL)
  764. cissys_free(data[i].asBuffer.buffer);
  765. }
  766. }
  767. cissys_free(data);
  768. }
  769. break;
  770. case CIS_CALLBACK_EXECUTE:
  771. {
  772. ECOMM_TRACE(UNILOG_PLA_APP, cisProcessThread_3, P_SIG, 0, "cisProcessThread CIS_CALLBACK_EXECUTE");
  773. prvExecResponse(gCisContext,&node->uri,node->param.asExec.buffer,node->param.asExec.length,node->mid);
  774. cissys_free(node->param.asExec.buffer);
  775. }
  776. break;
  777. case CIS_CALLBACK_SETPARAMS:
  778. {
  779. ECOMM_TRACE(UNILOG_PLA_APP, cisProcessThread_4, P_SIG, 0, "cisProcessThread CIS_CALLBACK_SETPARAMS");
  780. //set parameters and notify
  781. prvParamsResponse(gCisContext,&node->uri,node->param.asObserveParam.params,node->mid);
  782. }
  783. break;
  784. case CIS_CALLBACK_OBSERVE:
  785. {
  786. LOGD("observe callback mid=%d flag=%d\r\n", node->mid, node->param.asObserve.flag);
  787. if(node->param.asObserve.flag)
  788. {
  789. uint16_t count = 0;
  790. struct st_observe_info* observe_new = (struct st_observe_info*)cissys_malloc(sizeof(struct st_observe_info));
  791. observe_new->mid = node->mid;
  792. observe_new->uri = node->uri;
  793. observe_new->next = NULL;
  794. // mid change every time once register
  795. g_observeList = (struct st_observe_info*)cis_list_add((cis_list_t*)g_observeList,(cis_list_t*)observe_new);
  796. cis_response(gCisContext,NULL,NULL,node->mid,CIS_RESPONSE_OBSERVE, PS_SOCK_RAI_NO_INFO);
  797. ECOMM_TRACE(UNILOG_PLA_APP, cisProcessThread_5, P_INFO, 4, "cisProcessThread CIS_CALLBACK_OBSERVE set(%d): %d/%d/%d",
  798. count,
  799. observe_new->uri.objectId,
  800. CIS_URI_IS_SET_INSTANCE(&observe_new->uri)?observe_new->uri.instanceId:-1,
  801. CIS_URI_IS_SET_RESOURCE(&observe_new->uri)?observe_new->uri.resourceId:-1);
  802. }
  803. else
  804. {
  805. struct st_observe_info* delnode = g_observeList;
  806. while (delnode) {
  807. if (node->uri.flag == delnode->uri.flag && node->uri.objectId == delnode->uri.objectId) {
  808. if (node->uri.instanceId == delnode->uri.instanceId) {
  809. if (node->uri.resourceId == delnode->uri.resourceId) {
  810. break;
  811. }
  812. }
  813. }
  814. delnode = delnode->next;
  815. }
  816. if (delnode != NULL) {
  817. g_observeList = (struct st_observe_info *)cis_list_remove((cis_list_t *)g_observeList, delnode->mid, (cis_list_t **)&delnode);
  818. ECOMM_TRACE(UNILOG_PLA_APP, cisProcessThread_6, P_INFO, 3, "cis_on_observe cancel: %d/%d/%d\n",
  819. delnode->uri.objectId,
  820. CIS_URI_IS_SET_INSTANCE(&delnode->uri) ? delnode->uri.instanceId : -1,
  821. CIS_URI_IS_SET_RESOURCE(&delnode->uri) ? delnode->uri.resourceId : -1);
  822. cis_free(delnode);
  823. cis_response(gCisContext, NULL, NULL, node->mid, CIS_RESPONSE_OBSERVE, PS_SOCK_RAI_NO_INFO);
  824. }
  825. else {
  826. return;
  827. }
  828. }
  829. }
  830. break;
  831. default:
  832. break;
  833. }
  834. cissys_free(node);
  835. }
  836. }
  837. void cisAsyncUpdatePumpState(et_client_state_t state)
  838. {
  839. if(gOnenetContextRunning.pumpState != state)
  840. gOnenetContextRunning.pumpState = state;
  841. }
  842. void cisAsyncAddObject()
  843. {
  844. int index = 0;
  845. for (index= 0;index < SAMPLE_OBJECT_MAX ; index++)
  846. {
  847. cis_inst_bitmap_t bitmap;
  848. cis_res_count_t rescount;
  849. cis_instcount_t instCount,instBytes;
  850. const char* instAsciiPtr;
  851. uint8_t * instPtr;
  852. cis_oid_t oid;
  853. int16_t i;
  854. st_sample_object* obj = &g_objectList[index];
  855. oid = obj->oid;
  856. instCount = strlen(obj->instBitmap);
  857. instBytes = (instCount - 1) / 8 + 1;
  858. instAsciiPtr = obj->instBitmap;
  859. instPtr = (uint8_t*)cissys_malloc(instBytes);
  860. memset(instPtr,0,instBytes);
  861. for (i = 0;i < instCount;i++)
  862. {
  863. cis_instcount_t instBytePos = i / 8;
  864. cis_instcount_t instByteOffset = 7 - (i % 8);
  865. if(instAsciiPtr[i] == '1'){
  866. instPtr[instBytePos] += 0x01 << instByteOffset;
  867. }
  868. }
  869. bitmap.instanceCount = instCount;
  870. bitmap.instanceBitmap = instPtr;
  871. bitmap.instanceBytes = instBytes;
  872. rescount.attrCount = obj->attrCount;
  873. rescount.actCount = obj->actCount;
  874. cis_addobject(gCisContext,oid,&bitmap,&rescount);
  875. cissys_free(instPtr);
  876. }
  877. }
  878. void cisAsyncRegister()
  879. {
  880. g_lifetime = 36000;
  881. cis_callback_t callback;
  882. callback.onRead = cisAsynOnRead;
  883. callback.onWrite = cisAsynOnWrite;
  884. callback.onExec = cisAsynOnExec;
  885. callback.onObserve = cisAsynOnObserve;
  886. callback.onSetParams = cisAsynOnParams;
  887. callback.onEvent = cisAsynOnEvent;
  888. callback.onDiscover = cisAsynOnDiscover;
  889. cis_register(gCisContext,g_lifetime,&callback);
  890. }
  891. bool cisAsyncCheckNotificationReady()
  892. {
  893. if (gCisContext == NULL)
  894. return FALSE;
  895. else
  896. return ((((st_context_t*)gCisContext)->stateStep == PUMP_STATE_READY) &&
  897. (((st_context_t*)gCisContext)->observedList != NULL));
  898. }
  899. void cisAsyncBackup(void *pdata, slpManLpState state)
  900. {
  901. ECOMM_TRACE(UNILOG_PLA_APP, cisAsyncBackup_0, P_ERROR, 0, "Enter cisAsyncBackup");
  902. memcpy((uint8_t *)&(gOnenetContextRunning.gObservedBackup[0]), &(g_observed_backup[0]), MAX_OBSERVED_COUNT*sizeof(observed_backup_t));
  903. #if USE_LITTLEFS
  904. int32_t ret;
  905. lfs_file_t file;
  906. ret = LFS_FileOpen(&file, "cisSlpCntxt", LFS_O_WRONLY);
  907. OsaCheck(ret == 0,0,0,0);
  908. ret = LFS_FileWrite(&file, &gOnenetContextRunning, sizeof(onenet_context_t));
  909. OsaCheck(ret == sizeof(onenet_context_t),ret,0,0);
  910. LFS_FileClose(&file);
  911. #endif
  912. }
  913. void cisAsyncRestore()
  914. {
  915. uint8_t i;
  916. ECOMM_TRACE(UNILOG_PLA_APP, cisAsyncRestore_0, P_SIG, 0, "Enter cisAsyncRestore");
  917. #if USE_LITTLEFS
  918. int32_t ret;
  919. lfs_file_t file;
  920. ret = LFS_FileOpen(&file, "cisSlpCntxt", LFS_O_RDONLY | LFS_O_CREAT);
  921. OsaCheck(ret == 0,0,0,0);
  922. ret = LFS_FileRead(&file, &gOnenetContextRunning, sizeof(onenet_context_t));
  923. OsaCheck(ret >= 0,ret,0,0);
  924. LFS_FileClose(&file);
  925. #else
  926. memcpy((uint8_t *)&gOnenetContextRunning,(void*)APP_BACKUP_XIP_ADDR,sizeof(onenet_context_t));
  927. #endif
  928. // Restore for cis core using
  929. memcpy((uint8_t *)(&(g_observed_backup[0])), (uint8_t *)&(gOnenetContextRunning.gObservedBackup[0]), MAX_OBSERVED_COUNT*sizeof(observed_backup_t));
  930. // Restore for cis app using
  931. LOGD("observeObjNum =%d\r\n", gOnenetContextRunning.observeObjNum);
  932. for (i = 0; i < gOnenetContextRunning.observeObjNum; i++)
  933. {
  934. struct st_observe_info* observe_new = (struct st_observe_info*)cissys_malloc(sizeof(struct st_observe_info));
  935. memset((uint8_t *)observe_new, 0, sizeof(struct st_observe_info));
  936. observe_new->mid = g_observed_backup[i].msgid;
  937. LOGD("mid=%d\r\n", observe_new->mid);
  938. cis_memcpy(&(observe_new->uri), &(g_observed_backup[i].uri), sizeof(st_uri_t));
  939. cis_memcpy(&(observe_new->params), &(g_observed_backup[i].params), sizeof(cis_observe_attr_t));
  940. observe_new->next = NULL;
  941. g_observeList = (struct st_observe_info*)cis_list_add((cis_list_t*)g_observeList,(cis_list_t*)observe_new);
  942. }
  943. }
  944. void cisDataObserveReport()
  945. {
  946. uint32_t nowtime;
  947. /*data observe data report*/
  948. nowtime = cissys_gettime();
  949. struct st_observe_info* node = g_observeList;
  950. if (node == NULL)
  951. return;
  952. ECOMM_TRACE(UNILOG_PLA_APP, x1_cisDataObserveReport_0, P_INFO, 2, "cisDataObserveReport g_notifyLast=%d nowtime=%d", g_notifyLast, nowtime);
  953. // if diff time is more than 10S, continue to allow to report
  954. if((nowtime - g_notifyLast > 10*1000) && (!gNotifyOngoing))
  955. {
  956. g_notifyLast = nowtime;
  957. while(node != NULL)
  958. {
  959. if(node->mid == 0)
  960. {
  961. ECOMM_TRACE(UNILOG_PLA_APP, x1_cisDataObserveReport_1, P_INFO, 0, "cisDataObserveReport mid = 0");
  962. node = node->next;
  963. continue;
  964. }
  965. if(node->uri.flag == 0)
  966. {
  967. ECOMM_TRACE(UNILOG_PLA_APP, x1_cisDataObserveReport_2, P_INFO, 0, "cisDataObserveReport uri flag = 0");
  968. node = node->next;
  969. continue;
  970. }
  971. cis_uri_t uriLocal;
  972. uriLocal = node->uri;
  973. prvObserveNotify(gCisContext,&uriLocal,node->mid);
  974. node = node->next;
  975. ECOMM_TRACE(UNILOG_PLA_APP, x1_cisDataObserveReport_3, P_INFO, 0, "cisDataObserveReport");
  976. gNotifyOngoing = true;
  977. }
  978. }
  979. if ((nowtime - g_notifyLast > 20*100)&&(gNotifyOngoing))
  980. {
  981. ECOMM_TRACE(UNILOG_PLA_APP, x1_cisDataObserveReport_4, P_ERROR, 0, "cisDataObserveReport too long reset gNotifyOngoing");
  982. gNotifyOngoing = false;
  983. }
  984. }
  985. void cisAsyncProcess(void *arg)
  986. {
  987. if(cis_init(&gCisContext,(void *)config_hex,sizeof(config_hex),NULL, defaultLocalPort) != CIS_RET_OK){
  988. ECOMM_TRACE(UNILOG_PLA_APP, cis_sample_entry_1, P_ERROR, 0, "cis entry init failed.");
  989. LOGD("cis entry init failed.\n");
  990. if (gCisContext != NULL)
  991. cis_deinit(&gCisContext);
  992. return;
  993. }
  994. if(pmuBWakeupFromHib() || pmuBWakeupFromSleep2())
  995. {
  996. cisAsyncRestore();
  997. ECOMM_TRACE(UNILOG_PLA_APP, cis_sample_entry_0, P_INFO, 1, "wakeup from hib continue pumpState=%d", gOnenetContextRunning.pumpState);
  998. if (gOnenetContextRunning.pumpState == PUMP_STATE_READY || gOnenetContextRunning.pumpState == PUMP_STATE_CONNECTING)
  999. {
  1000. #if CIS_ENABLE_CONTEXT_RESTORE
  1001. ((st_context_t*)gCisContext)->ignoreRegistration = true;
  1002. ((st_context_t*)gCisContext)->stateStep = PUMP_STATE_CONNECTING;
  1003. #else
  1004. ((st_context_t*)gCisContext)->stateStep = PUMP_STATE_INITIAL;
  1005. #endif
  1006. cisAsyncUpdatePumpState(((st_context_t*)gCisContext)->stateStep);
  1007. }
  1008. observe_read_retention_data(gCisContext);
  1009. cisAsyncAddObject();
  1010. cisAsyncRegister();
  1011. }
  1012. else
  1013. {
  1014. cisAsyncAddObject();
  1015. cisAsyncRegister();
  1016. }
  1017. g_shutdown = false;
  1018. xTaskCreate(cisAsyncProcessTask,"cisAsyncProc", 512, NULL, osPriorityBelowNormal7, NULL);
  1019. while(1)
  1020. {
  1021. uint32_t nowtime;
  1022. if (g_shutdown)
  1023. {
  1024. cissys_sleepms(3000);
  1025. continue;
  1026. }
  1027. /*pump function*/
  1028. cis_pump(gCisContext);
  1029. cisAsyncUpdatePumpState(((st_context_t*)gCisContext)->stateStep);
  1030. cissys_sleepms(1000);
  1031. /*update lifetime*/
  1032. nowtime = cissys_gettime();
  1033. if(nowtime - g_lifetimeLast > ((g_lifetime * 1000) * 0.6)){
  1034. g_lifetimeLast = cissys_gettime();
  1035. cis_update_reg(gCisContext,LIFETIME_INVALID,false, PS_SOCK_RAI_NO_INFO);
  1036. }
  1037. }
  1038. }
  1039. void cisAsyncTask(void *arg)
  1040. {
  1041. while(1)
  1042. {
  1043. cisAsyncProcess(arg);
  1044. cissys_sleepms(1000);
  1045. }
  1046. }
  1047. void cisOnenetInit()
  1048. {
  1049. osThreadAttr_t task_attr;
  1050. slpManRet_t ret;
  1051. ret = slpManRegisterUsrdefinedBackupCb(cisAsyncBackup, NULL,SLPMAN_HIBERNATE_STATE);
  1052. OsaCheck(ret == RET_TRUE,0,0,0);
  1053. memset(&task_attr, 0, sizeof(osThreadAttr_t));
  1054. task_attr.name = "cisAsyncTask";
  1055. task_attr.stack_mem = onenetTaskStack;
  1056. task_attr.stack_size = ONENET_TASK_STACK_SIZE;
  1057. task_attr.priority = osPriorityBelowNormal7;
  1058. task_attr.cb_mem = &onenetTask;
  1059. task_attr.cb_size = sizeof(StaticTask_t);
  1060. memset(onenetTaskStack, 0xA5, ONENET_TASK_STACK_SIZE);
  1061. memset((uint8_t *)&gOnenetContextRunning, 0x00, sizeof(gOnenetContextRunning));
  1062. onenetTaskId = osThreadNew(cisAsyncTask, NULL,&task_attr);
  1063. }
  1064. void cisOnenetDeinit()
  1065. {
  1066. slpManUnregisterUsrdefinedBackupCb(cisAsyncBackup);
  1067. osThreadTerminate(onenetTaskId);
  1068. onenetTaskId = NULL;
  1069. }