module_jeff.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifdef ENABLE_JEFF
  6. #include "module_jeff.h"
  7. #include "jeff_export.h"
  8. #include "../vmcore_jeff/jeff-runtime.h"
  9. #include "../vmcore_jeff/jeff-thread.h"
  10. #include "../vmcore_jeff/jeff-buffer.h"
  11. #include "../vmcore_jeff/jeff-tool.h"
  12. #include "../vmcore_jeff/jeff-tool-priv.h"
  13. #include "app_manager-host.h"
  14. #include "bh_queue.h"
  15. #include "attr-container.h"
  16. #include "attr-container-util.h"
  17. #include "bh_thread.h"
  18. #include "ems_gc.h"
  19. #include "coap_ext.h"
  20. #include "libcore.h"
  21. #include "event.h"
  22. #include "watchdog.h"
  23. #define DEFAULT_APPLET_TIMEOUT (3 * 60 * 1000)
  24. #define DEFAULT_APPLET_HEAP_SIZE (48 * 1024)
  25. #define MIN_APPLET_HEAP_SIZE (2 * 1024)
  26. #define MAX_APPLET_HEAP_SIZE (1024 * 1024)
  27. typedef struct jeff_applet_data {
  28. /* Java Applet Object */
  29. JeffObjectRef applet_obj;
  30. #if BEIHAI_ENABLE_TOOL_AGENT != 0
  31. /* Whether the applet is in debug mode */
  32. bool debug_mode;
  33. /* Queue of the tool agent */
  34. bh_queue *tool_agent_queue;
  35. #endif
  36. /* VM instance */
  37. JeffInstanceLocalRoot *vm_instance;
  38. /* Applet Main file */
  39. JeffFileHeaderLinked *main_file;
  40. /* Permissions of the Java Applet */
  41. char *perms;
  42. } jeff_applet_data;
  43. /* Jeff class com.intel.aee.AEEApplet */
  44. static JeffClassHeaderLinked *class_AEEApplet;
  45. /* Jeff class com.intel.aee.Request */
  46. static JeffClassHeaderLinked *class_AEERequest;
  47. /* Jeff class com.intel.aee.Timer */
  48. static JeffClassHeaderLinked *class_Timer;
  49. /* Jeff class com.intel.aee.Sensor */
  50. static JeffClassHeaderLinked *class_Sensor;
  51. /* Jeff class com.intel.aee.ble.BLEManager */
  52. static JeffClassHeaderLinked *class_BLEManager;
  53. /* Jeff class com.intel.aee.ble.BLEDevice */
  54. static JeffClassHeaderLinked *class_BLEDevice;
  55. /* Jeff class com.intel.aee.ble.BLEGattService */
  56. JeffClassHeaderLinked *class_BLEGattService;
  57. /* Jeff class com.intel.aee.ble.BLEGattCharacteristic */
  58. JeffClassHeaderLinked *class_BLEGattCharacteristic;
  59. /* Jeff class com.intel.aee.ble.BLEGattDescriptor */
  60. JeffClassHeaderLinked *class_BLEGattDescriptor;
  61. /* Jeff class com.intel.aee.gpio.GPIOChannel */
  62. static JeffClassHeaderLinked *class_GPIOChannel;
  63. /* Jeff method void com.intel.aee.AEEApplet.onInit() */
  64. static JeffMethodLinked *method_AEEApplet_onInit;
  65. /* Jeff method void com.intel.aee.AEEApplet.onDestroy() */
  66. static JeffMethodLinked *method_AEEApplet_onDestroy;
  67. /* Jeff method void com.intel.aee.AEEApplet.callOnRequest(Request request) */
  68. static JeffMethodLinked *method_AEEApplet_callOnRequest;
  69. /* Jeff method void com.intel.aee.Timer.callOnTimer() */
  70. static JeffMethodLinked *method_callOnTimer;
  71. /* Jeff method void com.intel.aee.Sensor.callOnSensorEvent() */
  72. static JeffMethodLinked *method_callOnSensorEvent;
  73. /* Jeff method void com.intel.aee.ble.BLEManager.callOnBLEStartDiscovery() */
  74. static JeffMethodLinked *method_callOnBLEStartDiscovery;
  75. /* Jeff method void com.intel.aee.ble.BLEManager.callOnBLEConnected() */
  76. static JeffMethodLinked *method_callOnBLEConnected;
  77. /* Jeff method void com.intel.aee.ble.BLEManager.callOnBLEDisonnected() */
  78. static JeffMethodLinked *method_callOnBLEDisconnected;
  79. /* Jeff method void com.intel.aee.ble.BLEManager.callOnBLENotification() */
  80. static JeffMethodLinked *method_callOnBLENotification;
  81. /* Jeff method void com.intel.aee.ble.BLEManager.callOnBLEIndication() */
  82. static JeffMethodLinked *method_callOnBLEIndication;
  83. /* Jeff method void com.intel.aee.ble.BLEManager.callOnBLEPasskeyEntry() */
  84. static JeffMethodLinked *method_callOnBLEPasskeyEntry;
  85. /* Jeff method void com.intel.aee.gpio.GPIOChannel.callOnGPIOInterrupt() */
  86. static JeffMethodLinked *method_callOnGPIOInterrupt;
  87. /* Jeff method void com.intel.aee.ble.BLEManager.getBLEDevice() */
  88. static JeffMethodLinked *method_callOnBLEManagerGetBLEDevice;
  89. static jeff_applet_data *
  90. app_manager_get_jeff_applet_data()
  91. {
  92. module_data *m_data = app_manager_get_module_data(Module_Jeff);
  93. return (jeff_applet_data *)m_data->internal_data;
  94. }
  95. #if BEIHAI_ENABLE_TOOL_AGENT != 0
  96. void *
  97. app_manager_get_tool_agent_queue()
  98. {
  99. return app_manager_get_jeff_applet_data()->tool_agent_queue;
  100. }
  101. #endif
  102. #if BEIHAI_ENABLE_TOOL_AGENT != 0
  103. static bool
  104. is_tool_agent_running(module_data *m_data)
  105. {
  106. jeff_applet_data *applet_data = (jeff_applet_data *)m_data->internal_data;
  107. return (applet_data->debug_mode && applet_data->tool_agent_queue
  108. && applet_data->vm_instance->tool_agent);
  109. }
  110. #endif
  111. static char *
  112. get_class_qname(const JeffString *pname, const JeffString *cname)
  113. {
  114. unsigned int length =
  115. pname->length ? pname->length + 2 + cname->length : cname->length + 1;
  116. char *buf = APP_MGR_MALLOC(length), *p;
  117. if (!buf)
  118. return NULL;
  119. p = buf;
  120. if (pname->length) {
  121. bh_memcpy_s(p, pname->length, pname->value, pname->length);
  122. p += pname->length;
  123. *p++ = '.';
  124. }
  125. bh_memcpy_s(p, cname->length, cname->value, cname->length);
  126. p += cname->length;
  127. *p = '\0';
  128. return buf;
  129. }
  130. static void
  131. send_exception_event_to_host(const char *applet_name, const char *exc_name)
  132. {
  133. attr_container_t *payload;
  134. bh_request_msg_t msg;
  135. char *url;
  136. int url_len;
  137. payload = attr_container_create("exception detail");
  138. if (!payload) {
  139. app_manager_printf("Send exception to host fail: allocate memory");
  140. return;
  141. }
  142. if (!attr_container_set_string(&payload, "exception name", exc_name)
  143. || !attr_container_set_string(&payload, "stack trace", "TODO")
  144. || !attr_container_set_string(&payload, "applet name", applet_name)) {
  145. app_manager_printf("Send exception to host fail: set attr");
  146. goto fail;
  147. }
  148. url_len = strlen("/exception/") + strlen(applet_name);
  149. url = APP_MGR_MALLOC(url_len + 1);
  150. if (!url) {
  151. app_manager_printf("Send exception to host fail: allocate memory");
  152. goto fail;
  153. }
  154. memset(url, 0, url_len + 1);
  155. bh_strcpy_s(url, url_len + 1, "/exception/");
  156. bh_strcat_s(url, url_len + 1, applet_name);
  157. memset(&msg, 0, sizeof(msg));
  158. msg.url = url;
  159. msg.action = COAP_PUT;
  160. msg.payload = (char *)payload;
  161. app_send_request_msg_to_host(&msg);
  162. APP_MGR_FREE(url);
  163. fail:
  164. attr_container_destroy(payload);
  165. }
  166. static bool
  167. check_exception()
  168. {
  169. if (jeff_runtime_get_exception()) {
  170. jeff_printf("V1.Exception thrown when running applet '%s':\n",
  171. app_manager_get_module_name(Module_Jeff));
  172. jeff_runtime_print_exception();
  173. jeff_printf("\n");
  174. jeff_printf(NULL);
  175. }
  176. if (!app_manager_is_interrupting_module(Module_Jeff)) {
  177. attr_container_t *payload;
  178. int payload_len;
  179. JeffClassHeaderLinked *exc_class =
  180. jeff_object_class_pointer(jeff_runtime_get_exception());
  181. char *qname_buf = get_class_qname(jeff_get_class_pname(exc_class),
  182. jeff_get_class_cname(exc_class));
  183. /* Send exception event to host */
  184. if (qname_buf) {
  185. send_exception_event_to_host(
  186. app_manager_get_module_name(Module_Jeff), qname_buf);
  187. APP_MGR_FREE(qname_buf);
  188. }
  189. /* Uninstall the applet */
  190. if ((payload = attr_container_create("uninstall myself"))) {
  191. if (attr_container_set_string(
  192. &payload, "name", app_manager_get_module_name(Module_Jeff))
  193. /* Set special flag to prevent app manager making response
  194. since this is an internal message */
  195. && attr_container_set_bool(&payload, "do not reply me", true)) {
  196. request_t request = { 0 };
  197. payload_len = attr_container_get_serialize_length(payload);
  198. init_request(request, "/applet", COAP_DELETE, (char *)payload, payload_len));
  199. app_mgr_lookup_resource(&request);
  200. // TODO: confirm this is right
  201. attr_container_destroy(payload);
  202. }
  203. }
  204. jeff_runtime_set_exception(NULL);
  205. return true;
  206. }
  207. return false;
  208. }
  209. static bool
  210. app_manager_initialize_class(JeffClassHeaderLinked *c)
  211. {
  212. jeff_runtime_initialize_class(c);
  213. return !check_exception();
  214. }
  215. static bool
  216. app_manager_initialize_object(JeffObjectRef obj)
  217. {
  218. jeff_runtime_initialize_object(obj);
  219. return !check_exception();
  220. }
  221. static bool
  222. app_manager_call_java(JeffMethodLinked *method, unsigned int argc,
  223. uint32 argv[], uint8 argt[])
  224. {
  225. module_data *m_data = app_manager_get_module_data(Module_Jeff);
  226. watchdog_timer *wd_timer = &m_data->wd_timer;
  227. bool is_wd_started = false;
  228. #if BEIHAI_ENABLE_TOOL_AGENT != 0
  229. /* Only start watchdog when debugger is not running */
  230. if (!is_tool_agent_running(m_data)) {
  231. #endif
  232. watchdog_timer_start(wd_timer);
  233. is_wd_started = true;
  234. #if BEIHAI_ENABLE_TOOL_AGENT != 0
  235. }
  236. #endif
  237. jeff_runtime_call_java(method, argc, argv, argt);
  238. if (is_wd_started) {
  239. os_mutex_lock(&wd_timer->lock);
  240. if (!wd_timer->is_interrupting) {
  241. wd_timer->is_stopped = true;
  242. watchdog_timer_stop(wd_timer);
  243. }
  244. os_mutex_unlock(&wd_timer->lock);
  245. }
  246. return !check_exception();
  247. }
  248. static AEEBLEDevice
  249. create_object_BLEDevice(ble_device_info *dev_info)
  250. {
  251. JeffLocalObjectRef ref;
  252. AEEBLEDevice dev_struct;
  253. jeff_runtime_push_local_object_ref(&ref);
  254. ref.val = jeff_runtime_new_object(class_BLEDevice);
  255. if (!ref.val) {
  256. jeff_runtime_pop_local_object_ref(1);
  257. return NULL;
  258. }
  259. dev_struct = (AEEBLEDevice)(ref.val);
  260. dev_struct->rssi = dev_info->rssi;
  261. dev_struct->mac =
  262. (jbyteArray)jeff_runtime_create_byte_array((int8 *)dev_info->mac, 6);
  263. app_manager_printf("adv_data_len:%d,scan_response_len:%d\n",
  264. dev_info->adv_data_len, dev_info->scan_response_len);
  265. dev_struct->advData = (jbyteArray)jeff_runtime_create_byte_array(
  266. (int8 *)dev_info->adv_data, dev_info->adv_data_len);
  267. dev_struct->scanResponse = (jbyteArray)jeff_runtime_create_byte_array(
  268. (int8 *)dev_info->scan_response, dev_info->scan_response_len);
  269. dev_struct->addressType = dev_info->address_type;
  270. jeff_runtime_initialize_object(ref.val);
  271. jeff_runtime_pop_local_object_ref(1);
  272. if ((dev_struct->mac == NULL) || (dev_struct->advData == NULL)
  273. || (dev_struct->scanResponse == NULL)) {
  274. return NULL;
  275. }
  276. return (AEEBLEDevice)ref.val;
  277. }
  278. static void
  279. app_instance_process_ble_msg(char *msg)
  280. {
  281. bh_queue_ble_sub_msg_t *ble_msg = (bh_queue_ble_sub_msg_t *)msg;
  282. unsigned int argv[5];
  283. uint8 argt[5];
  284. ble_device_info *dev_info;
  285. dev_info = (ble_device_info *)ble_msg->payload;
  286. AEEBLEDevice ble_dev;
  287. argv[0] = (unsigned int)(jbyteArray)jeff_runtime_create_byte_array(
  288. (int8 *)dev_info->mac, 6);
  289. argt[0] = 1;
  290. if (!app_manager_call_java(method_callOnBLEManagerGetBLEDevice, 1, argv,
  291. argt)) {
  292. app_manager_printf(
  293. "app_manager_call_java BLEManagerGetBLEDevice fail error\n");
  294. goto fail;
  295. }
  296. ble_dev = (AEEBLEDevice)argv[0];
  297. if (ble_dev == NULL) {
  298. ble_dev = create_object_BLEDevice(dev_info);
  299. if (ble_dev == NULL) {
  300. goto fail;
  301. }
  302. }
  303. switch (ble_msg->type) {
  304. case BLE_SUB_EVENT_DISCOVERY:
  305. {
  306. argv[0] = (unsigned int)ble_dev;
  307. argt[0] = 1;
  308. ble_dev->rssi = dev_info->rssi;
  309. if (!app_manager_call_java(method_callOnBLEStartDiscovery, 1, argv,
  310. argt)) {
  311. app_manager_printf(
  312. "app_manager_call_java method_callOnBLEStartDiscovery "
  313. "fail error\n");
  314. goto fail;
  315. }
  316. break;
  317. }
  318. case BLE_SUB_EVENT_CONNECTED:
  319. {
  320. if (ble_dev) {
  321. argv[0] = (unsigned int)ble_dev;
  322. argv[1] = 0;
  323. argt[0] = 1;
  324. argt[1] = 1;
  325. if (!app_manager_call_java(method_callOnBLEConnected, 2, argv,
  326. argt)) {
  327. app_manager_printf(
  328. "app_manager_call_java method_callOnBLEConnected "
  329. "fail error\n");
  330. goto fail;
  331. }
  332. }
  333. break;
  334. }
  335. case BLE_SUB_EVENT_DISCONNECTED:
  336. {
  337. app_manager_printf("app instance received disconnected\n");
  338. if (ble_dev) {
  339. argv[0] = (unsigned int)ble_dev;
  340. argv[1] = 0;
  341. argt[0] = 1;
  342. argt[1] = 1;
  343. ble_dev->rssi = dev_info->rssi;
  344. if (!app_manager_call_java(method_callOnBLEDisconnected, 2,
  345. argv, argt)) {
  346. app_manager_printf(
  347. "app_manager_call_java "
  348. "method_callOnBLEDisconnected fail error\n");
  349. goto fail;
  350. }
  351. }
  352. break;
  353. }
  354. case BLE_SUB_EVENT_NOTIFICATION:
  355. {
  356. if (ble_dev) {
  357. argv[0] = (unsigned int)ble_dev;
  358. argv[1] =
  359. (unsigned int)(jbyteArray)jeff_runtime_create_byte_array(
  360. (int8 *)dev_info->private_data,
  361. dev_info->private_data_length);
  362. argv[2] = dev_info->value_handle;
  363. argv[3] = dev_info->ccc_handle;
  364. argt[1] = 1;
  365. argt[2] = 0;
  366. argt[3] = 0;
  367. ble_dev->rssi = dev_info->rssi;
  368. if (!app_manager_call_java(method_callOnBLENotification, 4,
  369. argv, argt)) {
  370. app_manager_printf(
  371. "app_manager_call_java "
  372. "method_callOnBLENotification fail error\n");
  373. goto fail;
  374. }
  375. }
  376. break;
  377. }
  378. case BLE_SUB_EVENT_INDICATION:
  379. {
  380. if (ble_dev) {
  381. argv[0] = (unsigned int)ble_dev;
  382. argv[1] =
  383. (unsigned int)(jbyteArray)jeff_runtime_create_byte_array(
  384. (int8 *)dev_info->private_data,
  385. dev_info->private_data_length);
  386. argv[2] = dev_info->value_handle;
  387. argv[3] = dev_info->ccc_handle;
  388. argt[0] = 1;
  389. argt[1] = 1;
  390. argt[2] = 0;
  391. argt[3] = 0;
  392. ble_dev->rssi = dev_info->rssi;
  393. if (!app_manager_call_java(method_callOnBLEIndication, 4, argv,
  394. argt)) {
  395. app_manager_printf(
  396. "app_manager_call_java method_callOnBLEIndication "
  397. "fail error\n");
  398. goto fail;
  399. }
  400. }
  401. break;
  402. }
  403. case BLE_SUB_EVENT_PASSKEYENTRY:
  404. {
  405. if (ble_dev) {
  406. argv[0] = (unsigned int)ble_dev;
  407. argt[0] = 1;
  408. argt[1] = 1;
  409. ble_dev->rssi = dev_info->rssi;
  410. if (!app_manager_call_java(method_callOnBLEPasskeyEntry, 1,
  411. argv, argt)) {
  412. app_manager_printf(
  413. "app_manager_call_java "
  414. "method_callOnBLEPasskeyEntry fail error\n");
  415. goto fail;
  416. }
  417. }
  418. break;
  419. }
  420. case BLE_SUB_EVENT_SECURITY_LEVEL_CHANGE:
  421. {
  422. if (ble_dev) {
  423. ble_dev->securityLevel = dev_info->security_level;
  424. }
  425. break;
  426. }
  427. default:
  428. break;
  429. }
  430. fail:
  431. if (dev_info->scan_response != NULL) {
  432. APP_MGR_FREE(dev_info->scan_response);
  433. }
  434. if (dev_info->private_data != NULL) {
  435. APP_MGR_FREE(dev_info->private_data);
  436. }
  437. if (dev_info->adv_data != NULL) {
  438. APP_MGR_FREE(dev_info->adv_data);
  439. }
  440. if (dev_info != NULL) {
  441. APP_MGR_FREE(dev_info);
  442. }
  443. }
  444. static void
  445. app_instance_free_ble_msg(char *msg)
  446. {
  447. bh_queue_ble_sub_msg_t *ble_msg = (bh_queue_ble_sub_msg_t *)msg;
  448. ble_device_info *dev_info;
  449. dev_info = (ble_device_info *)ble_msg->payload;
  450. if (dev_info->scan_response != NULL)
  451. APP_MGR_FREE(dev_info->scan_response);
  452. if (dev_info->private_data != NULL)
  453. APP_MGR_FREE(dev_info->private_data);
  454. if (dev_info->adv_data != NULL)
  455. APP_MGR_FREE(dev_info->adv_data);
  456. if (dev_info != NULL)
  457. APP_MGR_FREE(dev_info);
  458. }
  459. static void
  460. app_instance_queue_free_callback(void *queue_msg)
  461. {
  462. bh_queue_msg_t *msg = (bh_queue_msg_t *)queue_msg;
  463. switch (msg->message_type) {
  464. case APPLET_REQUEST:
  465. {
  466. bh_request_msg_t *req_msg = (bh_request_msg_t *)msg->payload;
  467. APP_MGR_FREE(req_msg);
  468. break;
  469. }
  470. case TIMER_EVENT:
  471. {
  472. break;
  473. }
  474. case SENSOR_EVENT:
  475. {
  476. if (msg->payload) {
  477. bh_sensor_event_t *sensor_event =
  478. (bh_sensor_event_t *)msg->payload;
  479. attr_container_t *event = sensor_event->event;
  480. attr_container_destroy(event);
  481. APP_MGR_FREE(sensor_event);
  482. }
  483. break;
  484. }
  485. case BLE_EVENT:
  486. {
  487. if (msg->payload) {
  488. app_instance_free_ble_msg(msg->payload);
  489. APP_MGR_FREE(msg->payload);
  490. }
  491. break;
  492. }
  493. case GPIO_INTERRUPT_EVENT:
  494. {
  495. break;
  496. }
  497. default:
  498. {
  499. break;
  500. }
  501. }
  502. APP_MGR_FREE(msg);
  503. }
  504. static void
  505. app_instance_queue_callback(void *queue_msg)
  506. {
  507. bh_queue_msg_t *msg = (bh_queue_msg_t *)queue_msg;
  508. unsigned int argv[5];
  509. uint8 argt[5];
  510. if (app_manager_is_interrupting_module(Module_Jeff)) {
  511. app_instance_queue_free_callback(queue_msg);
  512. return;
  513. }
  514. switch (msg->message_type) {
  515. case APPLET_REQUEST:
  516. {
  517. JeffLocalObjectRef ref;
  518. AEERequest req_obj;
  519. bh_request_msg_t *req_msg = (bh_request_msg_t *)msg->payload;
  520. attr_container_t *attr_cont = (attr_container_t *)req_msg->payload;
  521. module_data *m_data = app_manager_get_module_data(Module_Jeff);
  522. jeff_applet_data *applet_data =
  523. (jeff_applet_data *)m_data->internal_data;
  524. app_manager_printf("Applet %s got request, url %s, action %d\n",
  525. m_data->module_name, req_msg->url,
  526. req_msg->action);
  527. /* Create Request object */
  528. req_obj =
  529. (AEERequest)jeff_object_new(m_data->heap, class_AEERequest);
  530. if (!req_obj) {
  531. app_manager_printf("Applet process request failed: create "
  532. "request obj failed.\n");
  533. goto fail1;
  534. }
  535. jeff_runtime_push_local_object_ref(&ref);
  536. ref.val = (JeffObjectRef)req_obj;
  537. req_obj->mid = req_msg->mid;
  538. req_obj->action = req_msg->action;
  539. req_obj->fmt = req_msg->fmt;
  540. /* Create Java url string */
  541. if (req_msg->url) {
  542. req_obj->url =
  543. (jstring)jeff_runtime_create_java_string(req_msg->url);
  544. if (!req_obj->url) {
  545. app_manager_printf("Applet process request failed: "
  546. "create url string failed.\n");
  547. goto fail2;
  548. }
  549. }
  550. /* Create Java AttributeObject payload */
  551. if (attr_cont
  552. && !attr_container_to_attr_obj(attr_cont, &req_obj->payload)) {
  553. app_manager_printf("Applet process request failed: convert "
  554. "payload failed.\n");
  555. goto fail2;
  556. }
  557. /* Call AEEApplet.callOnRequest(Request request) method */
  558. argv[0] = (unsigned int)applet_data->applet_obj;
  559. argv[1] = (unsigned int)req_obj;
  560. argt[0] = argt[1] = 1;
  561. app_manager_call_java(method_AEEApplet_callOnRequest, 2, argv,
  562. argt);
  563. app_manager_printf("Applet process request success.\n");
  564. fail2:
  565. jeff_runtime_pop_local_object_ref(1);
  566. fail1:
  567. APP_MGR_FREE(req_msg);
  568. break;
  569. }
  570. case TIMER_EVENT:
  571. {
  572. if (msg->payload) {
  573. /* Call Timer.callOnTimer() method */
  574. argv[0] = (unsigned int)msg->payload;
  575. argt[0] = 1;
  576. app_manager_call_java(method_callOnTimer, 1, argv, argt);
  577. }
  578. break;
  579. }
  580. case SENSOR_EVENT:
  581. {
  582. if (msg->payload) {
  583. bh_sensor_event_t *sensor_event =
  584. (bh_sensor_event_t *)msg->payload;
  585. AEESensor sensor = sensor_event->sensor;
  586. attr_container_t *event = sensor_event->event;
  587. bool ret = attr_container_to_attr_obj(event, &sensor->event);
  588. attr_container_destroy(event);
  589. APP_MGR_FREE(sensor_event);
  590. if (ret) {
  591. /* Call Sensor.callOnSensorEvent() method */
  592. argv[0] = (unsigned int)sensor;
  593. argt[0] = 1;
  594. app_manager_call_java(method_callOnSensorEvent, 1, argv,
  595. argt);
  596. }
  597. }
  598. break;
  599. }
  600. case BLE_EVENT:
  601. {
  602. if (msg->payload) {
  603. app_instance_process_ble_msg(msg->payload);
  604. APP_MGR_FREE(msg->payload);
  605. }
  606. break;
  607. }
  608. case GPIO_INTERRUPT_EVENT:
  609. {
  610. AEEGPIOChannel gpio_ch = (AEEGPIOChannel)msg->payload;
  611. if ((gpio_ch == NULL) || (gpio_ch->callback == 0)
  612. || (gpio_ch->listener == NULL)) {
  613. break;
  614. }
  615. argv[0] = (unsigned int)gpio_ch;
  616. argt[0] = 1;
  617. bool ret_value = app_manager_call_java(method_callOnGPIOInterrupt,
  618. 1, argv, argt);
  619. if (!ret_value) {
  620. app_manager_printf(
  621. "app_manager_call_java "
  622. "method_method_callOnGPIOInterrupt return false\n");
  623. }
  624. break;
  625. }
  626. default:
  627. {
  628. app_manager_printf(
  629. "Invalid message type of applet queue message.\n");
  630. break;
  631. }
  632. }
  633. APP_MGR_FREE(msg);
  634. }
  635. static JeffClassHeaderLinked *
  636. find_main_class(JeffFileHeaderLinked *main_file)
  637. {
  638. JeffClassHeaderLinked *c = NULL, *ci;
  639. unsigned int i;
  640. for (i = 0; i < main_file->internal_class_count; i++) {
  641. ci = main_file->class_header[i];
  642. if (jeff_is_super_class(class_AEEApplet, ci)
  643. && (ci->access_flag & JEFF_ACC_PUBLIC)) {
  644. if (c) {
  645. jeff_printe_more_than_one_main_class();
  646. return NULL;
  647. }
  648. c = ci;
  649. }
  650. }
  651. if (!c)
  652. jeff_printe_no_main_class();
  653. return c;
  654. }
  655. /* Java applet thread main routine */
  656. static void *
  657. app_instance_main(void *arg)
  658. {
  659. module_data *m_data = (module_data *)arg;
  660. jeff_applet_data *applet_data = (jeff_applet_data *)m_data->internal_data;
  661. JeffClassHeaderLinked *object_class;
  662. JeffMethodLinked *m;
  663. unsigned int argv[1];
  664. uint8 argt[1];
  665. app_manager_printf("Java Applet '%s' started\n", m_data->module_name);
  666. #if BEIHAI_ENABLE_TOOL_AGENT != 0
  667. if (applet_data->debug_mode)
  668. jeff_tool_suspend_self();
  669. #endif
  670. applet_data->vm_instance->applet_object = applet_data->applet_obj;
  671. object_class = jeff_object_class_pointer(applet_data->applet_obj);
  672. m = jeff_select_method_virtual(object_class, method_AEEApplet_onInit);
  673. bh_assert(m != NULL);
  674. /* Initialize applet class which call <clinit> */
  675. if (!app_manager_initialize_class(object_class)) {
  676. app_manager_printf("Call <clinit> fail\n");
  677. goto fail;
  678. }
  679. /* Initialize applet object which call <init> */
  680. if (!app_manager_initialize_object(applet_data->applet_obj)) {
  681. app_manager_printf("Call <init> fail\n");
  682. goto fail;
  683. }
  684. /* Call applet's onInit() method */
  685. argv[0] = (unsigned int)applet_data->applet_obj;
  686. argt[0] = 1;
  687. if (app_manager_call_java(m, 1, argv, argt))
  688. /* Enter queue loop run to receive and process applet queue message
  689. */
  690. bh_queue_enter_loop_run(m_data->queue, app_instance_queue_callback);
  691. fail:
  692. applet_data->vm_instance->applet_object = applet_data->applet_obj;
  693. object_class = jeff_object_class_pointer(applet_data->applet_obj);
  694. m = jeff_select_method_virtual(object_class, method_AEEApplet_onDestroy);
  695. bh_assert(m != NULL);
  696. /* Call User Applet or AEEApplet onDestroy() method */
  697. app_manager_call_java(m, 1, argv, argt);
  698. if (m != method_AEEApplet_onDestroy) {
  699. /*If 'm' is user onDestroy, then Call AEEApplet.onDestroy() method*/
  700. app_manager_call_java(method_AEEApplet_onDestroy, 1, argv, argt);
  701. }
  702. app_manager_printf("Applet instance main thread exit.\n");
  703. return NULL;
  704. }
  705. static bool
  706. verify_signature(JeffFileHeader *file, unsigned size)
  707. {
  708. uint8 *sig;
  709. unsigned sig_size;
  710. #if BEIHAI_ENABLE_NO_SIGNATURE != 0
  711. /* no signature */
  712. if (file->file_signature == 0)
  713. return true;
  714. #endif
  715. if (file->file_length != size
  716. #if BEIHAI_ENABLE_NO_SIGNATURE == 0
  717. || file->file_signature == 0
  718. #endif
  719. || file->file_signature >= file->file_length)
  720. return false;
  721. sig = (uint8 *)file + file->file_signature;
  722. sig_size = file->file_length - file->file_signature;
  723. if (0
  724. == app_manager_signature_verify((uint8_t *)file, file->file_signature,
  725. sig, sig_size))
  726. return false;
  727. return true;
  728. }
  729. /* Install Java Applet */
  730. static bool
  731. jeff_module_install(bh_request_msg_t *msg)
  732. {
  733. unsigned int size, bpk_file_len, main_file_len, heap_size, timeout;
  734. uint8 *bpk_file;
  735. JeffFileHeaderLinked *main_file;
  736. JeffClassHeaderLinked *main_class;
  737. module_data *m_data;
  738. jeff_applet_data *applet_data;
  739. char *applet_name, *applet_perm;
  740. attr_container_t *attr_cont;
  741. bool debug = false;
  742. /* Check url */
  743. if (!msg->url || strcmp(msg->url, "/applet") != 0) {
  744. SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: invalid url.");
  745. return false;
  746. }
  747. /* Check payload */
  748. attr_cont = (attr_container_t *)msg->payload;
  749. if (!attr_cont
  750. || !(bpk_file = (uint8 *)attr_container_get_as_bytearray(
  751. attr_cont, "bpk", &bpk_file_len))) {
  752. SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: invalid bpk file.");
  753. return false;
  754. }
  755. /* Check applet name */
  756. applet_name = attr_container_get_as_string(attr_cont, "name");
  757. if (!applet_name || strlen(applet_name) == 0) {
  758. SEND_ERR_RESPONSE(msg->mid,
  759. "Install Applet failed: invalid applet name.");
  760. return false;
  761. }
  762. if (app_manager_lookup_module_data(applet_name)) {
  763. SEND_ERR_RESPONSE(msg->mid,
  764. "Install Applet failed: applet already installed.");
  765. return false;
  766. }
  767. /* TODO: convert bpk file to Jeff file */
  768. main_file_len = bpk_file_len;
  769. main_file = APP_MGR_MALLOC(main_file_len);
  770. if (!main_file) {
  771. SEND_ERR_RESPONSE(msg->mid,
  772. "Install Applet failed: allocate memory failed.");
  773. return false;
  774. }
  775. bh_memcpy_s(main_file, main_file_len, bpk_file, main_file_len);
  776. /* Verify signature */
  777. if (!verify_signature((JeffFileHeader *)main_file, main_file_len)) {
  778. SEND_ERR_RESPONSE(
  779. msg->mid,
  780. "Install Applet failed: verify Jeff file signature failed.");
  781. goto fail1;
  782. }
  783. /* Load Jeff main file */
  784. if (!jeff_runtime_load(main_file, main_file_len, false, NULL, NULL)) {
  785. SEND_ERR_RESPONSE(msg->mid,
  786. "Install Applet failed: load Jeff file failed.");
  787. goto fail1;
  788. }
  789. /* Find main class */
  790. main_class = find_main_class(main_file);
  791. if (!main_class) {
  792. SEND_ERR_RESPONSE(msg->mid,
  793. "Install Applet failed: find applet class failed.");
  794. goto fail2;
  795. }
  796. /* Create module data */
  797. size = offsetof(module_data, module_name) + strlen(applet_name) + 1;
  798. size = align_uint(size, 4);
  799. m_data = APP_MGR_MALLOC(size + sizeof(jeff_applet_data));
  800. if (!m_data) {
  801. SEND_ERR_RESPONSE(msg->mid,
  802. "Install Applet failed: allocate memory failed.");
  803. goto fail2;
  804. }
  805. memset(m_data, 0, size + sizeof(jeff_applet_data));
  806. m_data->module_type = Module_Jeff;
  807. m_data->internal_data = (uint8 *)m_data + size;
  808. applet_data = (jeff_applet_data *)m_data->internal_data;
  809. bh_strcpy_s(m_data->module_name, strlen(applet_name) + 1, applet_name);
  810. applet_data->main_file = main_file;
  811. /* Set applet execution timeout */
  812. timeout = DEFAULT_APPLET_TIMEOUT;
  813. if (attr_container_contain_key(attr_cont, "execution timeout"))
  814. timeout = attr_container_get_as_int(attr_cont, "execution timeout");
  815. m_data->timeout = timeout;
  816. /* Create applet permissions */
  817. applet_perm = attr_container_get_as_string(attr_cont, "perm");
  818. if (applet_perm != NULL) {
  819. applet_data->perms = APP_MGR_MALLOC(strlen(applet_perm) + 1);
  820. if (!applet_data->perms) {
  821. SEND_ERR_RESPONSE(msg->mid,
  822. "Install Applet failed: allocate memory for "
  823. "applet permissions failed.");
  824. goto fail3;
  825. }
  826. bh_strcpy_s(applet_data->perms, strlen(applet_perm) + 1, applet_perm);
  827. }
  828. /* Create applet queue */
  829. m_data->queue = bh_queue_create();
  830. if (!m_data->queue) {
  831. SEND_ERR_RESPONSE(msg->mid,
  832. "Install Applet failed: create applet queue failed.");
  833. goto fail3_1;
  834. }
  835. /* Set heap size */
  836. heap_size = DEFAULT_APPLET_HEAP_SIZE;
  837. if (attr_container_contain_key(attr_cont, "heap size")) {
  838. heap_size = attr_container_get_as_int(attr_cont, "heap size");
  839. if (heap_size < MIN_APPLET_HEAP_SIZE)
  840. heap_size = MIN_APPLET_HEAP_SIZE;
  841. else if (heap_size > MAX_APPLET_HEAP_SIZE)
  842. heap_size = MAX_APPLET_HEAP_SIZE;
  843. }
  844. m_data->heap_size = heap_size;
  845. /* Create applet heap */
  846. m_data->heap = gc_init_for_instance(heap_size);
  847. if (!m_data->heap) {
  848. SEND_ERR_RESPONSE(msg->mid,
  849. "Install Applet failed: create heap failed.");
  850. goto fail4;
  851. }
  852. /* Create applet object */
  853. applet_data->applet_obj = jeff_object_new(m_data->heap, main_class);
  854. if (!applet_data->applet_obj) {
  855. SEND_ERR_RESPONSE(
  856. msg->mid, "Install Applet failed: create applet object failed.");
  857. goto fail5;
  858. }
  859. /* Initialize watchdog timer */
  860. if (!watchdog_timer_init(m_data)) {
  861. SEND_ERR_RESPONSE(
  862. msg->mid,
  863. "Install Applet failed: create applet watchdog timer failed.");
  864. goto fail5;
  865. }
  866. #if BEIHAI_ENABLE_TOOL_AGENT != 0
  867. /* Check whether applet is debuggable */
  868. if (attr_container_contain_key(attr_cont, "debug"))
  869. debug = attr_container_get_as_bool(attr_cont, "debug");
  870. applet_data->debug_mode = debug;
  871. /* Create tool agent queue */
  872. if (debug && !(applet_data->tool_agent_queue = bh_queue_create())) {
  873. SEND_ERR_RESPONSE(
  874. msg->mid, "Install Applet failed: create tool agent queue failed.");
  875. goto fail5_1;
  876. }
  877. #endif
  878. /* Create applet instance */
  879. applet_data->vm_instance = jeff_runtime_create_instance(
  880. main_file, m_data->heap, 16, app_instance_main, m_data, NULL);
  881. if (!applet_data->vm_instance) {
  882. SEND_ERR_RESPONSE(msg->mid,
  883. "Install Applet failed: create Java VM failed");
  884. goto fail6;
  885. }
  886. /* Add applet data to applet data list */
  887. applet_data->vm_instance->applet_object = applet_data->applet_obj;
  888. app_manager_add_module_data(m_data);
  889. app_manager_post_applets_update_event();
  890. #if BEIHAI_ENABLE_TOOL_AGENT != 0
  891. /* Start tool agent thread */
  892. if (debug
  893. && !jeff_tool_start_agent(applet_data->vm_instance,
  894. applet_data->tool_agent_queue)) {
  895. SEND_ERR_RESPONSE(msg->mid,
  896. "Install Applet failed: start tool agent failed");
  897. goto fail6;
  898. }
  899. #endif
  900. app_manager_printf("Install Applet success!\n");
  901. app_send_response_to_host(msg->mid, CREATED_2_01, NULL); /* CREATED */
  902. return true;
  903. fail6:
  904. #if BEIHAI_ENABLE_TOOL_AGENT != 0
  905. if (debug)
  906. bh_queue_destroy(applet_data->tool_agent_queue);
  907. #endif
  908. fail5_1:
  909. watchdog_timer_destroy(&m_data->wd_timer);
  910. fail5:
  911. gc_destroy_for_instance(m_data->heap);
  912. fail4:
  913. bh_queue_destroy(m_data->queue, NULL);
  914. fail3_1:
  915. APP_MGR_FREE(applet_data->perms);
  916. fail3:
  917. APP_MGR_FREE(applet_data);
  918. fail2:
  919. jeff_runtime_unload(main_file);
  920. fail1:
  921. APP_MGR_FREE(main_file);
  922. return false;
  923. }
  924. static void
  925. cleanup_applet_resource(module_data *m_data)
  926. {
  927. jeff_applet_data *applet_data = (jeff_applet_data *)m_data->internal_data;
  928. /* Unload Jeff main file and free it */
  929. jeff_runtime_unload(applet_data->main_file);
  930. APP_MGR_FREE(applet_data->main_file);
  931. /* Destroy queue */
  932. bh_queue_destroy(m_data->queue, app_instance_queue_free_callback);
  933. /* Destroy heap */
  934. gc_destroy_for_instance(m_data->heap);
  935. /* Destroy watchdog timer */
  936. watchdog_timer_destroy(&m_data->wd_timer);
  937. /* Remove module data from module data list and free it */
  938. app_manager_del_module_data(m_data);
  939. APP_MGR_FREE(applet_data->perms);
  940. APP_MGR_FREE(m_data);
  941. }
  942. /* Uninstall Java Applet */
  943. static bool
  944. jeff_module_uninstall(bh_request_msg_t *msg)
  945. {
  946. module_data *m_data;
  947. jeff_applet_data *applet_data;
  948. attr_container_t *attr_cont;
  949. char *applet_name;
  950. bool do_not_reply = false;
  951. /* Check payload and applet name*/
  952. attr_cont = (attr_container_t *)msg->payload;
  953. /* Check whether need to reply this request */
  954. if (attr_container_contain_key(attr_cont, "do not reply me"))
  955. do_not_reply = attr_container_get_as_bool(attr_cont, "do not reply me");
  956. /* Check url */
  957. if (!msg->url || strcmp(msg->url, "/applet") != 0) {
  958. if (!do_not_reply)
  959. SEND_ERR_RESPONSE(msg->mid,
  960. "Uninstall Applet failed: invalid url.");
  961. else
  962. app_manager_printf("Uninstall Applet failed: invalid url.");
  963. return false;
  964. }
  965. if (!attr_cont
  966. || !(applet_name = attr_container_get_as_string(attr_cont, "name"))
  967. || strlen(applet_name) == 0) {
  968. if (!do_not_reply)
  969. SEND_ERR_RESPONSE(msg->mid,
  970. "Uninstall Applet failed: invalid applet name.");
  971. else
  972. app_manager_printf("Uninstall Applet failed: invalid applet name.");
  973. return false;
  974. }
  975. m_data = app_manager_lookup_module_data(applet_name);
  976. if (!m_data) {
  977. if (!do_not_reply)
  978. SEND_ERR_RESPONSE(msg->mid,
  979. "Uninstall Applet failed: no applet found.");
  980. else
  981. app_manager_printf("Uninstall Applet failed: no applet found.");
  982. return false;
  983. }
  984. if (m_data->module_type != Module_Jeff) {
  985. if (!do_not_reply)
  986. SEND_ERR_RESPONSE(msg->mid,
  987. "Uninstall Applet failed: invlaid module type.");
  988. else
  989. app_manager_printf("Uninstall Applet failed: invalid module type.");
  990. return false;
  991. }
  992. if (m_data->wd_timer.is_interrupting) {
  993. if (!do_not_reply)
  994. SEND_ERR_RESPONSE(msg->mid,
  995. "Uninstall Applet failed: applet is being "
  996. "interrupted by watchdog.");
  997. else
  998. app_manager_printf("Uninstall Applet failed: applet is being "
  999. "interrupted by watchdog.");
  1000. return false;
  1001. }
  1002. /* Exit applet queue loop run */
  1003. bh_queue_exit_loop_run(m_data->queue);
  1004. applet_data = (jeff_applet_data *)m_data->internal_data;
  1005. #if BEIHAI_ENABLE_TOOL_AGENT != 0
  1006. /* Exit tool agent queue loop run */
  1007. if (is_tool_agent_running(m_data)) {
  1008. bh_queue_exit_loop_run(applet_data->tool_agent_queue);
  1009. }
  1010. #endif
  1011. /* Wait the end of the applet instance and then destroy it */
  1012. if (applet_data->vm_instance->main_file)
  1013. jeff_runtime_wait_for_instance(applet_data->vm_instance, -1);
  1014. jeff_runtime_destroy_instance(applet_data->vm_instance);
  1015. cleanup_applet_resource(m_data);
  1016. app_manager_post_applets_update_event();
  1017. app_manager_printf("Uninstall Applet success!\n");
  1018. if (!do_not_reply)
  1019. app_send_response_to_host(msg->mid, DELETED_2_02, NULL); /* DELETED */
  1020. return true;
  1021. }
  1022. #define PERM_PREFIX "AEE.permission."
  1023. static bool
  1024. check_permission_format(const char *perm)
  1025. {
  1026. const char *prefix = PERM_PREFIX;
  1027. const char *p;
  1028. if (perm == NULL || strncmp(perm, prefix, strlen(prefix)) != 0
  1029. || *(p = perm + strlen(prefix)) == '\0')
  1030. return false;
  1031. do {
  1032. if (!(*p == '.' || ('A' <= *p && *p <= 'Z')
  1033. || ('a' <= *p && *p <= 'z')))
  1034. return false;
  1035. } while (*++p != '\0');
  1036. return true;
  1037. }
  1038. static bool
  1039. match(const char *haystack, const char *needle, char delim)
  1040. {
  1041. const char *p = needle;
  1042. if (haystack == NULL || *haystack == '\0' || needle == NULL
  1043. || *needle == '\0')
  1044. return false;
  1045. while (true) {
  1046. while (true) {
  1047. if ((*haystack == '\0' || *haystack == delim) && *p == '\0') {
  1048. return true;
  1049. }
  1050. else if (*p == *haystack) {
  1051. ++p;
  1052. ++haystack;
  1053. }
  1054. else {
  1055. break;
  1056. }
  1057. }
  1058. while (*haystack != '\0' && *haystack != delim) {
  1059. ++haystack;
  1060. }
  1061. if (*haystack == '\0') {
  1062. return false;
  1063. }
  1064. else {
  1065. ++haystack;
  1066. p = needle;
  1067. }
  1068. }
  1069. }
  1070. bool
  1071. bh_applet_check_permission(const char *perm)
  1072. {
  1073. return check_permission_format(perm)
  1074. && match(app_manager_get_jeff_applet_data()->perms,
  1075. perm + strlen(PERM_PREFIX), ' ');
  1076. }
  1077. static bool
  1078. jeff_module_init()
  1079. {
  1080. JeffDescriptorFull d[] = { { JEFF_TYPE_VOID, 0, NULL } };
  1081. JeffDescriptorFull d1[] = { { JEFF_TYPE_OBJECT | JEFF_TYPE_REF, 0, NULL },
  1082. { JEFF_TYPE_VOID, 0, NULL } };
  1083. /* Resolve class com.intel.aee.AEEApplet */
  1084. class_AEEApplet =
  1085. jeff_runtime_resolve_class_full_name("com.intel.aee.AEEApplet");
  1086. if (!class_AEEApplet) {
  1087. app_manager_printf(
  1088. "App Manager start failed: resolve class AEEApplet failed.\n");
  1089. return false;
  1090. }
  1091. /* Resolve class com.intel.aee.Request */
  1092. class_AEERequest =
  1093. jeff_runtime_resolve_class_full_name("com.intel.aee.Request");
  1094. if (!class_AEERequest) {
  1095. app_manager_printf(
  1096. "App Manager start failed: resolve class Request failed.\n");
  1097. return false;
  1098. }
  1099. /* Resolve class com.intel.aee.Timer */
  1100. class_Timer = jeff_runtime_resolve_class_full_name("com.intel.aee.Timer");
  1101. if (!class_Timer) {
  1102. app_manager_printf(
  1103. "App Manager start failed: resolve class Timer failed.\n");
  1104. return false;
  1105. }
  1106. /* Resolve class com.intel.aee.Sensor */
  1107. class_Sensor = jeff_runtime_resolve_class_full_name("com.intel.aee.Sensor");
  1108. if (!class_Sensor) {
  1109. app_manager_printf(
  1110. "App Manager start failed: resolve class Sensor failed.\n");
  1111. return false;
  1112. }
  1113. /* Resolve class com.intel.aee.ble.BLEManager */
  1114. class_BLEManager =
  1115. jeff_runtime_resolve_class_full_name("com.intel.aee.ble.BLEManager");
  1116. if (!class_BLEManager) {
  1117. app_manager_printf(
  1118. "App Manager start failed: resolve class BLEManager failed.\n");
  1119. return false;
  1120. }
  1121. /* Resolve class com.intel.aee.ble.BLEDevice */
  1122. class_BLEDevice =
  1123. jeff_runtime_resolve_class_full_name("com.intel.aee.ble.BLEDevice");
  1124. if (!class_BLEDevice) {
  1125. app_manager_printf(
  1126. "App Manager start failed: resolve class BLEDevice failed.\n");
  1127. return false;
  1128. }
  1129. /* Resolve class com.intel.aee.ble.BLEDevice */
  1130. class_BLEGattService = jeff_runtime_resolve_class_full_name(
  1131. "com.intel.aee.ble.BLEGattService");
  1132. if (!class_BLEGattService) {
  1133. app_manager_printf("App Manager start failed: resolve class "
  1134. "BLEGattService failed.\n");
  1135. return false;
  1136. }
  1137. /* Resolve class com.intel.aee.ble.BLEDevice */
  1138. class_BLEGattCharacteristic = jeff_runtime_resolve_class_full_name(
  1139. "com.intel.aee.ble.BLEGattCharacteristic");
  1140. if (!class_BLEGattCharacteristic) {
  1141. app_manager_printf("App Manager start failed: resolve class "
  1142. "BLEGattCharacteristic failed.\n");
  1143. return false;
  1144. }
  1145. /* Resolve class com.intel.aee.ble.BLEDevice */
  1146. class_BLEGattDescriptor = jeff_runtime_resolve_class_full_name(
  1147. "com.intel.aee.ble.BLEGattDescriptor");
  1148. if (!class_BLEGattDescriptor) {
  1149. app_manager_printf("App Manager start failed: resolve class "
  1150. "BLEGattDescriptor failed.\n");
  1151. return false;
  1152. }
  1153. /* Resolve class com.intel.aee.gpio.GPIOChannel */
  1154. class_GPIOChannel =
  1155. jeff_runtime_resolve_class_full_name("com.intel.aee.gpio.GPIOChannel");
  1156. if (!class_GPIOChannel) {
  1157. app_manager_printf("App Manager start failed: resolve class "
  1158. "GPIOChannel failed.\n");
  1159. return false;
  1160. }
  1161. /* Resolve method com.intel.aee.AEEApplet.onInit() */
  1162. method_AEEApplet_onInit =
  1163. jeff_lookup_method(class_AEEApplet, "onInit", 0, d);
  1164. if (!method_AEEApplet_onInit) {
  1165. app_manager_printf("App Manager start failed: resolve method "
  1166. "Applet.onInit() failed.\n");
  1167. return false;
  1168. }
  1169. /* Resolve method com.intel.aee.AEEApplet.onDestroy() */
  1170. method_AEEApplet_onDestroy =
  1171. jeff_lookup_method(class_AEEApplet, "onDestroy", 0, d);
  1172. if (!method_AEEApplet_onDestroy) {
  1173. app_manager_printf("App Manager start failed: resolve method "
  1174. "AEEApplet.onDestroy() failed.\n");
  1175. return false;
  1176. }
  1177. /* Resolve method com.intel.aee.AEEApplet.callOnRequest(Request) */
  1178. d1[0].class_header = class_AEERequest;
  1179. method_AEEApplet_callOnRequest =
  1180. jeff_lookup_method(class_AEEApplet, "callOnRequest", 1, d1);
  1181. if (!method_AEEApplet_callOnRequest) {
  1182. app_manager_printf("App Manager start failed: resolve method "
  1183. "AEEApplet.callOnRequest() failed.\n");
  1184. return false;
  1185. }
  1186. /* Resolve method com.intel.aee.Timer.callOnTimer() */
  1187. method_callOnTimer = jeff_lookup_method(class_Timer, "callOnTimer", 0, d);
  1188. if (!method_callOnTimer) {
  1189. app_manager_printf("App Manager start failed: resolve method "
  1190. "Timer.callOnTimer() failed.\n");
  1191. return false;
  1192. }
  1193. /* Resolve method com.intel.aee.Sensor.callOnSensorEvent() */
  1194. method_callOnSensorEvent =
  1195. jeff_lookup_method(class_Sensor, "callOnSensorEvent", 0, d);
  1196. if (!method_callOnSensorEvent) {
  1197. app_manager_printf("App Manager start failed: resolve method "
  1198. "Sensor.callOnSensorEvent() failed.\n");
  1199. return false;
  1200. }
  1201. /* Resovle method
  1202. * com.intel.aee.ble.BLEManager.callOnBLEStartDiscovery(BLEDevice) */
  1203. d1[0].class_header = class_BLEDevice;
  1204. method_callOnBLEStartDiscovery =
  1205. jeff_lookup_method(class_BLEManager, "callOnBLEStartDiscovery", 1, d1);
  1206. if (!method_callOnBLEStartDiscovery) {
  1207. app_manager_printf("App Manager start failed: resolve method "
  1208. "BLEManager.callOnBLEStartDiscovery() failed.\n");
  1209. return false;
  1210. }
  1211. /* Resovle method
  1212. * com.intel.aee.ble.BLEManager.callOnBLEConnected(BLEDevice) */
  1213. JeffDescriptorFull d2_1[] = { { JEFF_TYPE_OBJECT | JEFF_TYPE_REF, 0,
  1214. class_BLEDevice },
  1215. { JEFF_TYPE_INT, 0, NULL },
  1216. { JEFF_TYPE_VOID, 0, NULL } };
  1217. method_callOnBLEConnected =
  1218. jeff_lookup_method(class_BLEManager, "callOnBLEConnected", 2, d2_1);
  1219. if (!method_callOnBLEConnected) {
  1220. app_manager_printf("App Manager start failed: resolve method "
  1221. "BLEManager.callOnBLEConnected() failed.\n");
  1222. return false;
  1223. }
  1224. /* Resovle method
  1225. * com.intel.aee.ble.BLEManager.method_callOnBLENotification(BLEDevice,byte[])
  1226. */
  1227. JeffDescriptorFull d2_2[] = {
  1228. { JEFF_TYPE_OBJECT | JEFF_TYPE_REF, 0, class_BLEDevice },
  1229. { JEFF_TYPE_BYTE | JEFF_TYPE_REF | JEFF_TYPE_MONO, 1, NULL },
  1230. { JEFF_TYPE_INT, 0, NULL },
  1231. { JEFF_TYPE_INT, 0, NULL },
  1232. { JEFF_TYPE_VOID, 0, NULL }
  1233. };
  1234. method_callOnBLENotification =
  1235. jeff_lookup_method(class_BLEManager, "callOnBLENotification", 4, d2_2);
  1236. if (!method_callOnBLENotification) {
  1237. app_manager_printf("App Manager start failed: resolve method "
  1238. "BLEManager.callOnBLENotification() failed.\n");
  1239. return false;
  1240. }
  1241. /* Resovle method
  1242. * com.intel.aee.ble.BLEManager.callOnBLEConnected(BLEDevice,byte[]) */
  1243. method_callOnBLEIndication =
  1244. jeff_lookup_method(class_BLEManager, "callOnBLEIndication", 4, d2_2);
  1245. if (!method_callOnBLEIndication) {
  1246. app_manager_printf("App Manager start failed: resolve method "
  1247. "BLEManager.callOnBLEIndication() failed.\n");
  1248. return false;
  1249. }
  1250. /* Resovle method
  1251. * com.intel.aee.ble.BLEManager.callOnBLEConnected(BLEDevice) */
  1252. d1[0].class_header = class_BLEDevice;
  1253. method_callOnBLEDisconnected =
  1254. jeff_lookup_method(class_BLEManager, "callOnBLEDisconnected", 1, d1);
  1255. if (!method_callOnBLEDisconnected) {
  1256. app_manager_printf("App Manager start failed: resolve method "
  1257. "BLEManager.callOnBLEDisconnected() failed.\n");
  1258. return false;
  1259. }
  1260. /* Resovle method
  1261. * com.intel.aee.ble.BLEManager.callOnBLEConnected(BLEDevice) */
  1262. method_callOnBLEPasskeyEntry =
  1263. jeff_lookup_method(class_BLEManager, "callOnBLEPasskeyEntry", 1, d1);
  1264. if (!method_callOnBLEPasskeyEntry) {
  1265. app_manager_printf("App Manager start failed: resolve method "
  1266. "BLEManager.callOnBLEPasskeyEntry() failed.\n");
  1267. return false;
  1268. }
  1269. /* Resovle method void
  1270. * com.intel.aee.gpio.GPIOChannel.callOnGPIOInterrupt() */
  1271. method_callOnGPIOInterrupt =
  1272. jeff_lookup_method(class_GPIOChannel, "callOnGPIOInterrupt", 0, d);
  1273. if (!method_callOnGPIOInterrupt) {
  1274. app_manager_printf("App Manager start failed: resolve method "
  1275. "GPIOChannel.callOnGPIOInterrupt() failed.\n");
  1276. return false;
  1277. }
  1278. JeffDescriptorFull d2[] = {
  1279. { JEFF_TYPE_BYTE | JEFF_TYPE_REF | JEFF_TYPE_MONO, 1, NULL },
  1280. { JEFF_TYPE_OBJECT | JEFF_TYPE_REF, 0, class_BLEDevice }
  1281. };
  1282. /* Resovle method com.intel.aee.ble.BLEManager.getBLEDevice(byte []) */
  1283. method_callOnBLEManagerGetBLEDevice =
  1284. jeff_lookup_method(class_BLEManager, "getBLEDevice", 1, d2);
  1285. if (!method_callOnBLEManagerGetBLEDevice) {
  1286. app_manager_printf("App Manager start failed: resolve method "
  1287. "BLEManager.getBLEDevice() failed.\n");
  1288. return false;
  1289. }
  1290. return true;
  1291. }
  1292. static void
  1293. jeff_module_watchdog_kill(module_data *m_data)
  1294. {
  1295. jeff_applet_data *applet_data = (jeff_applet_data *)m_data->internal_data;
  1296. app_manager_printf("Watchdog interrupt the applet %s\n",
  1297. m_data->module_name);
  1298. jeff_runtime_interrupt_instance(applet_data->vm_instance, true);
  1299. /* Exit applet queue loop run */
  1300. bh_queue_exit_loop_run(m_data->queue);
  1301. /* Wait the end of the applet instance. If timeout, it means applet
  1302. * is busy executing native code, then try to cancle the main thread. */
  1303. if (applet_data->vm_instance->main_file)
  1304. jeff_runtime_wait_for_instance(applet_data->vm_instance, 3000);
  1305. if (applet_data->vm_instance->main_file) {
  1306. app_manager_printf("Watchdog cancel applet main thread.\n");
  1307. os_thread_cancel(applet_data->vm_instance->main_tlr.handle);
  1308. /* k_thread_abort(applet_data->vm_instance->main_tlr.handle); */
  1309. }
  1310. send_exception_event_to_host(m_data->module_name,
  1311. "java.lang.InterruptedException");
  1312. cleanup_applet_resource(m_data);
  1313. app_manager_printf("Watchdog interrupt Jeff applet done.\n");
  1314. }
  1315. static bool
  1316. jeff_module_handle_host_url(void *queue_msg)
  1317. {
  1318. #if BEIHAI_ENABLE_TOOL_AGENT != 0
  1319. bh_queue_msg_t *msg = (bh_queue_msg_t *)queue_msg;
  1320. if (msg->message_type == COAP_PARSED) {
  1321. coap_packet_t *packet = (coap_packet_t *)msg->payload;
  1322. attr_container_t *attr_cont = (attr_container_t *)packet->payload;
  1323. const char *url = NULL;
  1324. int url_len = 0, mid;
  1325. bh_memcpy_s(&mid, sizeof(uint32), packet->token, sizeof(uint32));
  1326. url_len = coap_get_header_uri_path(packet, &url);
  1327. /* Send request to tool agent */
  1328. if (url_len >= 12 && memcmp(url, "/tool_agent/", 12) == 0) {
  1329. module_data *m_data;
  1330. jeff_applet_data *applet_data;
  1331. unsigned attr_cont_len = 0, req_msg_len;
  1332. bh_queue_msg_t *tool_agent_msg;
  1333. bh_request_msg_t *req_msg;
  1334. char url_buf[256] = { 0 }, *p = url_buf;
  1335. char applet_name[128] = { 0 };
  1336. /* Resolve applet name */
  1337. bh_memcpy_s(url_buf, sizeof(url_buf), url + 12, url_len - 12);
  1338. while (*p != '/' && *p != '\0')
  1339. p++;
  1340. bh_memcpy_s(applet_name, sizeof(applet_name), url_buf, p - url_buf);
  1341. app_manager_printf("Send request to tool agent of applet: %s\n",
  1342. applet_name);
  1343. /* Check applet name */
  1344. if (!(m_data = app_manager_lookup_module_data(applet_name))) {
  1345. SEND_ERR_RESPONSE(mid, "Send request to tool agent failed: "
  1346. "invalid applet name");
  1347. return false;
  1348. }
  1349. applet_data = (jeff_applet_data *)m_data->internal_data;
  1350. /* Attach debug: start the tool agent firstly */
  1351. if (packet->code == COAP_PUT) {
  1352. if (is_tool_agent_running(m_data)) {
  1353. SEND_ERR_RESPONSE(mid, "Attach debug failed: tool "
  1354. "agent is already exist.");
  1355. return false;
  1356. }
  1357. applet_data->debug_mode = true;
  1358. /* Create tool agent queue */
  1359. if (!(applet_data->tool_agent_queue = bh_queue_create())) {
  1360. SEND_ERR_RESPONSE(mid, "Attach debug failed: create "
  1361. "tool agent queue failed.");
  1362. return false;
  1363. }
  1364. /* Start tool agent thread */
  1365. if (!jeff_tool_start_agent(applet_data->vm_instance,
  1366. applet_data->tool_agent_queue)) {
  1367. bh_queue_destroy(applet_data->tool_agent_queue, NULL);
  1368. SEND_ERR_RESPONSE(
  1369. mid, "Attach debug failed: start tool agent failed");
  1370. return false;
  1371. }
  1372. app_manager_printf("Attach debug: start tool agent of "
  1373. "applet %s success.\n",
  1374. applet_name);
  1375. app_send_response_to_host(mid, CREATED_2_01, NULL); /* OK */
  1376. }
  1377. else {
  1378. /* Check tool agent running */
  1379. if (!is_tool_agent_running(m_data)) {
  1380. SEND_ERR_RESPONSE(mid, "Send request to tool agent failed: "
  1381. "tool agent is not running");
  1382. return false;
  1383. }
  1384. /* Create queue message for tool agent */
  1385. if (!(tool_agent_msg =
  1386. APP_MGR_MALLOC(sizeof(bh_queue_msg_t)))) {
  1387. SEND_ERR_RESPONSE(mid, "Send request to tool agent failed: "
  1388. "allocate memory failed");
  1389. return false;
  1390. }
  1391. if (attr_cont)
  1392. attr_cont_len =
  1393. attr_container_get_serialize_length(attr_cont);
  1394. req_msg_len =
  1395. sizeof(bh_request_msg_t) + strlen(p) + 1 + attr_cont_len;
  1396. /* Create request message */
  1397. if (!(req_msg = APP_MGR_MALLOC(req_msg_len))) {
  1398. SEND_ERR_RESPONSE(mid, "Send request to applet failed: "
  1399. "allocate memory failed");
  1400. APP_MGR_FREE(tool_agent_msg);
  1401. return false;
  1402. }
  1403. /* Set request message */
  1404. memset(req_msg, 0, req_msg_len);
  1405. req_msg->mid = mid;
  1406. req_msg->url = (char *)req_msg + sizeof(bh_request_msg_t);
  1407. bh_strcpy_s(req_msg->url, strlen(p) + 1,
  1408. p); /* Actual url sent to tool agent */
  1409. req_msg->action = packet->code;
  1410. req_msg->fmt = 0;
  1411. if (attr_cont) {
  1412. req_msg->payload = (char *)req_msg
  1413. + sizeof(bh_request_msg_t) + strlen(p)
  1414. + 1;
  1415. attr_container_serialize(req_msg->payload, attr_cont);
  1416. }
  1417. /* Set queue message and send to tool agent's queue */
  1418. tool_agent_msg->message_type = JDWP_REQUEST;
  1419. tool_agent_msg->payload_size = req_msg_len;
  1420. tool_agent_msg->payload = (char *)req_msg;
  1421. if (!bh_queue_send_message(applet_data->tool_agent_queue,
  1422. tool_agent_msg)) {
  1423. APP_MGR_FREE(req_msg);
  1424. APP_MGR_FREE(tool_agent_msg);
  1425. SEND_ERR_RESPONSE(mid, "Send request to tool agent failed: "
  1426. "send queue msg failed.");
  1427. return false;
  1428. }
  1429. /* app_manager_printf("Send request to tool agent of applet
  1430. * %s success.\n", applet_name); */
  1431. }
  1432. return true;
  1433. }
  1434. }
  1435. #endif /* BEIHAI_ENABLE_TOOL_AGENT != 0 */
  1436. return false;
  1437. }
  1438. static module_data *
  1439. jeff_module_get_module_data(void)
  1440. {
  1441. JeffThreadLocalRoot *self = jeff_runtime_get_tlr();
  1442. return (module_data *)self->il_root->start_routine_arg;
  1443. }
  1444. #if BEIHAI_ENABLE_TOOL_AGENT != 0
  1445. #define JDWP_HANDSHAKE_MAGIC "JDWP-Handshake"
  1446. #define JDWP_HANDSHAKE_LEN (sizeof(JDWP_HANDSHAKE_MAGIC) - 1)
  1447. #define JDWP_PAYLOAD_KEY "jdwp"
  1448. static bool debug = true;
  1449. static bool
  1450. send_msg_to_host(int mid, const char *url, int code, const uint8 *msg,
  1451. unsigned size)
  1452. {
  1453. bool ret;
  1454. int payload_len = 0;
  1455. attr_container_t *payload = NULL;
  1456. if (msg) {
  1457. if ((payload = attr_container_create(""))) {
  1458. attr_container_set_bytearray(&payload, JDWP_PAYLOAD_KEY,
  1459. (const int8_t *)msg, size);
  1460. payload_len = attr_container_get_serialize_length(payload);
  1461. }
  1462. }
  1463. ret = app_send_msg_to_host(mid, url, code, (char *)payload, payload_len);
  1464. if (payload)
  1465. attr_container_destroy(payload);
  1466. return ret;
  1467. }
  1468. static bool
  1469. send_response(int mid, int code, const uint8 *msg, unsigned size)
  1470. {
  1471. return send_msg_to_host(mid, NULL, code, msg, size);
  1472. }
  1473. static bool
  1474. send_packet_response(int mid, int code, JeffBuffer *packet)
  1475. {
  1476. int size;
  1477. if ((size = jeff_buffer_size(packet)) == 0)
  1478. /* No data need to be written, succeed. */
  1479. return true;
  1480. return send_msg_to_host(mid, NULL, code, jeff_buffer_at(packet, 0), size);
  1481. }
  1482. void
  1483. jeff_tool_event_publish(uint8 *evtbuf, unsigned size)
  1484. {
  1485. char *prefix = "/jdwp/", *url = NULL;
  1486. int url_len;
  1487. url_len = strlen(prefix) + strlen(app_manager_get_module_name(Module_Jeff));
  1488. if (NULL == (url = jeff_runtime_malloc(url_len + 1)))
  1489. return;
  1490. bh_strcpy_s(url, url_len + 1, prefix);
  1491. bh_strcat_s(url, url_len + 1, app_manager_get_module_name(Module_Jeff));
  1492. /* Event is sent as request so we set code as COAP_PUT */
  1493. if (event_is_registered(url))
  1494. send_msg_to_host(0, url, COAP_PUT, evtbuf, size);
  1495. jeff_runtime_free(url);
  1496. }
  1497. #define SEND_ERROR_RESPONSE(err_msg) \
  1498. do { \
  1499. app_manager_printf("%s\n", err_msg); \
  1500. send_response(req_msg->mid, INTERNAL_SERVER_ERROR_5_00, \
  1501. (uint8 *)err_msg, strlen(err_msg) + 1); \
  1502. } while (0)
  1503. /* Queue callback of tool agent */
  1504. void
  1505. tool_agent_queue_callback(void *arg)
  1506. {
  1507. bh_queue_msg_t *msg = (bh_queue_msg_t *)arg;
  1508. if (msg->message_type == JDWP_REQUEST) {
  1509. bh_request_msg_t *req_msg = (bh_request_msg_t *)msg->payload;
  1510. attr_container_t *attr_cont = (attr_container_t *)req_msg->payload;
  1511. JeffThreadLocalRoot *self = jeff_runtime_get_tlr();
  1512. JeffInstanceLocalRoot *cur_instance = self->il_root;
  1513. JeffToolAgent *agent = cur_instance->tool_agent;
  1514. bh_queue *queue = (bh_queue *)self->start_routine_arg;
  1515. if (debug)
  1516. app_manager_printf(
  1517. "Tool Agent of applet %s got request, url %s, action %d\n",
  1518. app_manager_get_module_name(Module_Jeff), req_msg->url,
  1519. req_msg->action);
  1520. /* Handshake or Process Request */
  1521. if (req_msg->action == COAP_GET) {
  1522. uint8 *buf;
  1523. unsigned buf_len;
  1524. if (!attr_cont
  1525. || !(buf = (uint8 *)attr_container_get_as_bytearray(
  1526. attr_cont, JDWP_PAYLOAD_KEY, &buf_len))) {
  1527. SEND_ERROR_RESPONSE("Tool Agent fail: invalid JDWP payload.");
  1528. goto fail;
  1529. }
  1530. if (!agent->connected) {
  1531. if (buf_len != JDWP_HANDSHAKE_LEN
  1532. || memcmp(buf, JDWP_HANDSHAKE_MAGIC, JDWP_HANDSHAKE_LEN)) {
  1533. SEND_ERROR_RESPONSE("Tool Agent fail: handshake fail.");
  1534. goto fail;
  1535. }
  1536. /* Handshake success and response */
  1537. agent->connected = true;
  1538. send_response(req_msg->mid, CONTENT_2_05, buf, buf_len);
  1539. }
  1540. else {
  1541. /* TODO: tool-agent thread should reuse the request/reply
  1542. * buffer to avoid allocating memory repeatedly */
  1543. JeffBuffer request, reply;
  1544. /* Initialize the package buffers. */
  1545. jeff_buffer_init(&request);
  1546. jeff_buffer_init(&reply);
  1547. if (!jeff_buffer_resize(&request, buf_len)) {
  1548. SEND_ERROR_RESPONSE("Tool Agent fail: resize buffer fail.");
  1549. jeff_buffer_destroy(&request);
  1550. jeff_buffer_destroy(&reply);
  1551. goto fail;
  1552. }
  1553. /* Copy data from request to jeff buffer */
  1554. bh_memcpy_s(jeff_buffer_at(&request, 0),
  1555. jeff_buffer_size(&request), buf, buf_len);
  1556. /* Handle JDWP request */
  1557. if (!jeff_tool_handle_packet(agent, &request, &reply)) {
  1558. SEND_ERROR_RESPONSE(
  1559. "Tool agent fail: handle request fail.");
  1560. jeff_buffer_destroy(&request);
  1561. jeff_buffer_destroy(&reply);
  1562. goto fail;
  1563. }
  1564. /* Response JDWP reply */
  1565. send_packet_response(req_msg->mid, CONTENT_2_05, &reply);
  1566. /* Destroy the package buffers. */
  1567. jeff_buffer_destroy(&request);
  1568. jeff_buffer_destroy(&reply);
  1569. }
  1570. }
  1571. /* Debugger disconnect */
  1572. else if (req_msg->action == COAP_DELETE) {
  1573. send_response(req_msg->mid, DELETED_2_02, NULL, 0);
  1574. bh_queue_exit_loop_run(queue);
  1575. }
  1576. else {
  1577. SEND_ERROR_RESPONSE("Tool agent fail: invalid request.");
  1578. goto fail;
  1579. }
  1580. APP_MGR_FREE(req_msg);
  1581. APP_MGR_FREE(msg);
  1582. return;
  1583. fail:
  1584. bh_queue_exit_loop_run(queue);
  1585. APP_MGR_FREE(req_msg);
  1586. }
  1587. APP_MGR_FREE(msg);
  1588. }
  1589. void
  1590. tool_agent_queue_free_callback(void *message)
  1591. {
  1592. bh_queue_msg_t *msg = (bh_queue_msg_t *)message;
  1593. if (msg->message_type == JDWP_REQUEST) {
  1594. bh_request_msg_t *req_msg = (bh_request_msg_t *)msg->payload;
  1595. APP_MGR_FREE(req_msg);
  1596. }
  1597. APP_MGR_FREE(msg);
  1598. }
  1599. #endif /* BEIHAI_ENABLE_TOOL_AGENT != 0 */
  1600. /* clang-format off */
  1601. module_interface jeff_module_interface = {
  1602. jeff_module_init,
  1603. jeff_module_install,
  1604. jeff_module_uninstall,
  1605. jeff_module_watchdog_kill,
  1606. jeff_module_handle_host_url,
  1607. jeff_module_get_module_data,
  1608. NULL
  1609. };
  1610. /* clang-format on */
  1611. #endif