ota_backend_bt.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief OTA bluetooth backend interface
  9. */
  10. #include <kernel.h>
  11. #include <string.h>
  12. #include <stream.h>
  13. #include <soc.h>
  14. #include <mem_manager.h>
  15. #include <ota_backend.h>
  16. #include <ota_backend_bt.h>
  17. #include <bt_manager.h>
  18. #include <crc.h>
  19. /* support check data checksum in each ota unit */
  20. #define CONFIG_OTA_BT_SUPPORT_UNIT_CRC
  21. #define TLV_TYPE_OTA_SUPPORT_FEATURES 0x09
  22. #define TLV_TYPE_OTA_TYPE 0x0A
  23. #define OTA_SUPPORT_FEATURE_UNIT_DATA_CRC (1 << 0)
  24. #define OTA_ERROR_CODE_SUCCESS 100000
  25. #define OTA_ERROR_CODE_OTA_TYPE_FAIL 100010
  26. #define SERVICE_ID_OTA 0x9
  27. #define OTA_SVC_SEND_BUFFER_SIZE 0x80
  28. #define OTA_UNIT_SIZE 0x100
  29. #define OTA_UNIT_FRCOMM_SIZE (650) // SPP Recommended Value <= (670-10 = 660)
  30. #define OTA_UNIT_BLE_SIZE (230) // BLE Recommended value <= (244-10 = 234)
  31. #define OTA_IOS_BLE_MAX_PKT (50) // IOS BLE Recommended value <= 60
  32. #define OTA_SPPBLE_BUFF_SIZE (OTA_UNIT_FRCOMM_SIZE*6 + 200) // OTA requires 6 block sizes
  33. #define TLV_MAX_DATA_LENGTH 0x3fff
  34. #define TLV_TYPE_ERROR_CODE 0x7f
  35. #define TLV_TYPE_MAIN 0x80
  36. #define OTA_CMD_H2D_REQUEST_UPGRADE 0x01
  37. #define OTA_CMD_H2D_CONNECT_NEGOTIATION 0x02
  38. #define OTA_CMD_D2H_REQUIRE_IMAGE_DATA 0x03
  39. #define OTA_CMD_H2D_SEND_IMAGE_DATA 0x04
  40. #define OTA_CMD_D2H_REPORT_RECEVIED_DATA_COUNT 0x05
  41. #define OTA_CMD_D2H_VALIDATE_IMAGE 0x06
  42. #define OTA_CMD_D2H_REPORT_STATUS 0x07
  43. #define OTA_CMD_D2H_CANCEL_UPGRADE 0x08
  44. #define OTA_CMD_H2D_NEGOTIATION_RESULT 0x09
  45. #define OTA_CMD_D2H_REQUEST_UPGRADE 0x0A
  46. #define OTA_CMD_H2D_SEND_IMAGE_DATA_WITH_CRC 0x0B
  47. #define TLV_PACK_U8(buf, type, value) tlv_pack_data(buf, type, sizeof(uint8_t), value)
  48. #define TLV_PACK_U16(buf, type, value) tlv_pack_data(buf, type, sizeof(uint16_t), value)
  49. #define TLV_PACK_U32(buf, type, value) tlv_pack_data(buf, type, sizeof(uint32_t), value)
  50. #define TLV_UNPACK_U8(ctx, type, value_ptr) \
  51. tlv_unpack_data(ctx, type, NULL, (uint8_t *)value_ptr, sizeof(uint8_t))
  52. #define TLV_UNPACK_U16(ctx, type, value_ptr) \
  53. tlv_unpack_data(ctx, type, NULL, (uint8_t *)value_ptr, sizeof(uint16_t))
  54. #define TLV_UNPACK_U32(ctx, type, value_ptr) \
  55. tlv_unpack_data(ctx, type, NULL, (uint8_t *)value_ptr, sizeof(uint32_t))
  56. #ifdef CONFIG_BT_BLE
  57. #ifdef CONFIG_BT_BLE_APP_UPDATE_PARAM
  58. #ifdef CONFIG_SYS_WAKELOCK
  59. #include <sys_wakelock.h>
  60. #define ble_ota_wake_lock() sys_wake_lock(PARTIAL_WAKE_LOCK)
  61. #define ble_ota_wake_unlock() sys_wake_unlock(PARTIAL_WAKE_LOCK)
  62. #else
  63. #define ble_ota_wake_lock()
  64. #define ble_ota_wake_unlock()
  65. #endif
  66. static os_delayed_work ble_ota_uparam_work;
  67. #endif
  68. #endif
  69. enum svc_prot_state{
  70. PROT_STATE_IDLE,
  71. PROT_STATE_DATA,
  72. };
  73. struct svc_prot_context {
  74. uint8_t state;
  75. int send_buf_size;
  76. uint8_t *send_buf;
  77. uint8_t *read_buf;
  78. int read_len;
  79. int read_offset;
  80. int read_done_len;
  81. int read_buf_len;
  82. uint8_t last_psn;
  83. uint8_t host_features;
  84. uint16_t ota_unit_size;
  85. io_stream_t sppble_stream;
  86. int sppble_stream_opened;
  87. int negotiation_done;
  88. ota_backend_type_cb_t ota_type_cb;
  89. int ota_type; // 0: app ota 1: factory ota 2: PC config ota
  90. uint8_t connect_type;
  91. };
  92. struct svc_prot_head {
  93. uint8_t svc_id;
  94. uint8_t cmd;
  95. uint8_t param_type;
  96. uint16_t param_len;
  97. } __attribute__((packed));
  98. struct tlv_head {
  99. uint8_t type;
  100. uint16_t len;
  101. uint8_t value[0];
  102. } __attribute__((packed));
  103. struct tlv_data {
  104. uint8_t type;
  105. uint16_t len;
  106. uint16_t max_len;
  107. uint8_t *value_ptr;
  108. };
  109. typedef int (*svc_prot_cmd_handler_t)(struct svc_prot_context *ctx, uint16_t param_len);
  110. struct svc_prot_cmd {
  111. uint8_t cmd;
  112. svc_prot_cmd_handler_t handler;
  113. };
  114. struct ota_backend_bt {
  115. struct ota_backend backend;
  116. struct svc_prot_context svc_ctx;
  117. };
  118. /* for sppble_stream callback */
  119. static struct ota_backend_bt *g_backend_bt;
  120. static int svc_prot_get_rx_data(struct svc_prot_context *ctx, uint8_t *buf, int size)
  121. {
  122. int read_size;
  123. while (size > 0) {
  124. read_size = stream_read(ctx->sppble_stream, buf, size);
  125. if (read_size <= 0) {
  126. SYS_LOG_ERR("need read %d bytes, but only got %d bytes",
  127. size, read_size);
  128. return -EIO;
  129. }
  130. size -= read_size;
  131. buf += read_size;
  132. }
  133. return 0;
  134. }
  135. static int svc_prot_skip_rx_data(struct svc_prot_context *ctx, int size)
  136. {
  137. int err;
  138. uint8_t c;
  139. while (size > 0) {
  140. err = svc_prot_get_rx_data(ctx, &c, sizeof(uint8_t));
  141. if (err) {
  142. SYS_LOG_ERR("failed to get data");
  143. return err;
  144. }
  145. size--;
  146. }
  147. return 0;
  148. }
  149. static int svc_drop_all_rx_data(struct svc_prot_context *ctx, int wait_ms)
  150. {
  151. int err, data_len;
  152. int ms = wait_ms;
  153. while (wait_ms > 0) {
  154. data_len = stream_tell(ctx->sppble_stream);
  155. if (data_len > 0) {
  156. SYS_LOG_INF("drop data len 0x%x", data_len);
  157. err = svc_prot_skip_rx_data(ctx, data_len);
  158. if (err)
  159. return err;
  160. wait_ms = ms;
  161. }
  162. os_sleep(20);
  163. wait_ms -= 20;
  164. }
  165. return 0;
  166. }
  167. static uint8_t *tlv_pack(uint8_t *buf, const struct tlv_data *tlv)
  168. {
  169. uint16_t len;
  170. len = tlv->len;
  171. *buf++ = tlv->type;
  172. *buf++ = len & 0xff;
  173. *buf++ = len >> 8;
  174. if (tlv->value_ptr) {
  175. memcpy(buf, tlv->value_ptr, len);
  176. buf += len;
  177. }
  178. return buf;
  179. }
  180. static uint8_t *tlv_pack_data(uint8_t *buf, uint8_t type, uint16_t len, uint32_t number)
  181. {
  182. struct tlv_data tlv;
  183. tlv.type = type;
  184. tlv.len = len;
  185. tlv.value_ptr = (uint8_t *)&number;
  186. return tlv_pack(buf, &tlv);
  187. }
  188. static int tlv_unpack_head(struct svc_prot_context *ctx, struct tlv_data *tlv)
  189. {
  190. int err, total_len;
  191. /* read type */
  192. err = svc_prot_get_rx_data(ctx, &tlv->type, sizeof(tlv->type));
  193. if (err) {
  194. SYS_LOG_ERR("failed to read tlv type");
  195. return err;
  196. }
  197. /* read length */
  198. err = svc_prot_get_rx_data(ctx, (uint8_t *)&tlv->len, sizeof(tlv->len));
  199. if (err) {
  200. SYS_LOG_ERR("failed to read tlv type");
  201. return err;
  202. }
  203. total_len = sizeof(tlv->type) + sizeof(tlv->len);
  204. return total_len;
  205. }
  206. static int tlv_unpack(struct svc_prot_context *ctx, struct tlv_data *tlv)
  207. {
  208. uint16_t data_len;
  209. int err, total_len;
  210. total_len = tlv_unpack_head(ctx, tlv);
  211. if (tlv->len <= 0)
  212. return total_len;
  213. data_len = tlv->len;
  214. if (data_len > tlv->max_len) {
  215. data_len = tlv->max_len;
  216. }
  217. /* read value */
  218. if (tlv->value_ptr) {
  219. err = svc_prot_get_rx_data(ctx, tlv->value_ptr, data_len);
  220. if (err) {
  221. SYS_LOG_ERR("failed to read tlv value");
  222. return err;
  223. }
  224. total_len += data_len;
  225. }
  226. return total_len;
  227. }
  228. static int tlv_unpack_data(struct svc_prot_context *ctx, uint8_t type, int *len,
  229. uint8_t *value_ptr, int max_len)
  230. {
  231. struct tlv_data tlv;
  232. int rlen;
  233. tlv.value_ptr = value_ptr;
  234. tlv.max_len = max_len;
  235. rlen = tlv_unpack(ctx, &tlv);
  236. if (rlen < 0) {
  237. SYS_LOG_ERR("tlv_unpack failed, err 0x%x", rlen);
  238. return rlen;
  239. }
  240. if (tlv.type != type) {
  241. SYS_LOG_ERR("unmatched type, need 0x%x but got 0x%x", type, tlv.type);
  242. return -EIO;
  243. }
  244. if (len)
  245. *len = tlv.len;
  246. return rlen;
  247. }
  248. int svc_prot_send_data(struct svc_prot_context *ctx, uint8_t *buf, int size)
  249. {
  250. int err;
  251. err = stream_write(ctx->sppble_stream, buf, size);
  252. if (err < 0) {
  253. SYS_LOG_ERR("failed to send data, size %d, err %d", size, err);
  254. return -EIO;
  255. }
  256. return 0;
  257. }
  258. int ota_bt_send_cmd(struct svc_prot_context *ctx, uint8_t cmd,
  259. uint8_t *buf, int size)
  260. {
  261. struct svc_prot_head *head;
  262. int err;
  263. head = (struct svc_prot_head *)buf;
  264. head->svc_id = SERVICE_ID_OTA;
  265. head->cmd = cmd;
  266. head->param_type = TLV_TYPE_MAIN;
  267. head->param_len = size - sizeof(struct svc_prot_head);
  268. //SYS_LOG_INF("send cmd: %d", cmd);
  269. //print_buffer(buf, 1, size, 16, buf);
  270. err = svc_prot_send_data(ctx, buf, size);
  271. if (err) {
  272. SYS_LOG_ERR("failed to send cmd %d, err %d", cmd, err);
  273. return err;
  274. }
  275. return 0;
  276. }
  277. int ota_cmd_h2d_request_upgrade(struct svc_prot_context *ctx, uint16_t param_size)
  278. {
  279. int err = 0, send_len, battery_threahold, head_len, ota_type;
  280. uint8_t *send_buf;
  281. struct tlv_data tlv;
  282. uint8_t host_features, device_features;
  283. SYS_LOG_INF("upgrade request: param_size %d", param_size);
  284. if (1 == ctx->negotiation_done) {
  285. SYS_LOG_WRN("request upgrade cmd repeated.");
  286. } else {
  287. ctx->host_features = 0;
  288. ctx->ota_type = PROT_OTA_PHONE_APP;
  289. device_features = 0;
  290. }
  291. while (param_size > 0) {
  292. head_len = tlv_unpack_head(ctx, &tlv);
  293. if (head_len <= 0)
  294. return -EIO;
  295. switch (tlv.type) {
  296. case TLV_TYPE_OTA_SUPPORT_FEATURES:
  297. err = svc_prot_get_rx_data(ctx, &host_features, 1);
  298. if (err) {
  299. SYS_LOG_ERR("failed to read tlv value");
  300. return err;
  301. }
  302. ctx->host_features = host_features;
  303. SYS_LOG_INF("host support features: 0x%x", host_features);
  304. break;
  305. case TLV_TYPE_OTA_TYPE:
  306. err = svc_prot_get_rx_data(ctx, (uint8_t *)&ota_type, 4);
  307. if (err) {
  308. SYS_LOG_ERR("failed to read tlv value");
  309. return err;
  310. }
  311. ctx->ota_type = ota_type;
  312. SYS_LOG_INF("ota type: 0x%x", ota_type);
  313. break;
  314. default:
  315. /* skip other parameters by now */
  316. err = svc_prot_skip_rx_data(ctx, tlv.len);
  317. if (err)
  318. return -EIO;
  319. break;
  320. }
  321. param_size -= head_len + tlv.len;
  322. }
  323. if (1 == ctx->negotiation_done) {
  324. return 0;
  325. }
  326. ctx->state = PROT_STATE_IDLE;
  327. /* dummy value, for debug only */
  328. battery_threahold = 30;
  329. /* init ack data */
  330. send_buf = ctx->send_buf + sizeof(struct svc_prot_head);
  331. if (ctx->ota_type_cb)
  332. err = ctx->ota_type_cb(ctx->ota_type);
  333. if (err) {
  334. send_buf = TLV_PACK_U32(send_buf, TLV_TYPE_ERROR_CODE, OTA_ERROR_CODE_OTA_TYPE_FAIL);
  335. } else {
  336. send_buf = TLV_PACK_U32(send_buf, TLV_TYPE_ERROR_CODE, OTA_ERROR_CODE_SUCCESS);
  337. }
  338. send_buf = TLV_PACK_U8(send_buf, 0x04, battery_threahold);
  339. /* send device features only if host has support features */
  340. if (ctx->host_features) {
  341. #ifdef CONFIG_OTA_BT_SUPPORT_UNIT_CRC
  342. device_features |= OTA_SUPPORT_FEATURE_UNIT_DATA_CRC;
  343. #endif
  344. send_buf = TLV_PACK_U8(send_buf, TLV_TYPE_OTA_SUPPORT_FEATURES, device_features);
  345. SYS_LOG_INF("device support features: 0x%x", 0);
  346. }
  347. send_len = send_buf - ctx->send_buf;
  348. err = ota_bt_send_cmd(ctx, OTA_CMD_H2D_REQUEST_UPGRADE, ctx->send_buf, send_len);
  349. if (err) {
  350. SYS_LOG_ERR("failed to send cmd %d, error %d",
  351. OTA_CMD_H2D_REQUEST_UPGRADE, err);
  352. return err;
  353. }
  354. return err;
  355. }
  356. int ota_cmd_h2d_connect_negotiation(struct svc_prot_context *ctx, uint16_t param_len)
  357. {
  358. uint16_t app_wait_timeout, device_restart_timeout, ota_unit_size, interval;
  359. uint8_t ack_enable;
  360. uint8_t *send_buf;
  361. int err, send_len;
  362. if (param_len != 0) {
  363. SYS_LOG_ERR("read_param_len 0, real param_len %d\n", param_len);
  364. }
  365. if (0 == ctx->negotiation_done) {
  366. ctx->state = PROT_STATE_IDLE;
  367. }
  368. app_wait_timeout = 3;
  369. device_restart_timeout = 5;
  370. ota_unit_size = ctx->ota_unit_size;
  371. interval = 0;
  372. ack_enable= 0;
  373. SYS_LOG_INF("send app_wait_timeout %d, device_restart_timeout %d\n",
  374. app_wait_timeout, device_restart_timeout);
  375. SYS_LOG_INF("ota_unit_size %d, interval %d, ack_enable %d\n",
  376. ota_unit_size, interval, ack_enable);
  377. send_buf = ctx->send_buf + sizeof(struct svc_prot_head);
  378. send_buf = TLV_PACK_U16(send_buf, 0x1, app_wait_timeout);
  379. send_buf = TLV_PACK_U16(send_buf, 0x2, device_restart_timeout);
  380. send_buf = TLV_PACK_U16(send_buf, 0x3, ota_unit_size);
  381. send_buf = TLV_PACK_U16(send_buf, 0x4, interval);
  382. send_buf = TLV_PACK_U8(send_buf, 0x5, ack_enable);
  383. send_len = send_buf - ctx->send_buf;
  384. if (1 == ctx->negotiation_done) {
  385. SYS_LOG_WRN("connect negotiation cmd repeated.");
  386. return 0;
  387. }
  388. err = ota_bt_send_cmd(ctx, OTA_CMD_H2D_CONNECT_NEGOTIATION, ctx->send_buf, send_len);
  389. if (err) {
  390. SYS_LOG_ERR("failed to send cmd %d, error %d",
  391. OTA_CMD_H2D_CONNECT_NEGOTIATION, err);
  392. return err;
  393. }
  394. return err;
  395. }
  396. int ota_cmd_h2d_negotiation_result(struct svc_prot_context *ctx, uint16_t param_len)
  397. {
  398. uint8_t negotiation_result;
  399. int rlen;
  400. rlen = TLV_UNPACK_U8(ctx, 0x1, &negotiation_result);
  401. if (rlen < 0) {
  402. return -EIO;
  403. }
  404. if (1 == ctx->negotiation_done) {
  405. SYS_LOG_WRN("negotiation result cmd repeated.");
  406. return 0;
  407. }
  408. if (rlen != param_len) {
  409. SYS_LOG_ERR("read param len %d, but real param_len %d\n", rlen, param_len);
  410. }
  411. SYS_LOG_INF("negotiation_result %d\n", negotiation_result);
  412. ctx->negotiation_done = 1;
  413. ctx->state = PROT_STATE_IDLE;
  414. return 0;
  415. }
  416. int ota_cmd_require_image_data(struct svc_prot_context *ctx, uint32_t offset, int len, uint8_t *buf)
  417. {
  418. uint8_t read_mask, *send_buf;
  419. int err, send_len;
  420. SYS_LOG_DBG("offset 0x%x, len %d, buf %p, \n", offset, len, buf);
  421. if (!ctx->negotiation_done) {
  422. SYS_LOG_ERR("negotiation not done");
  423. return -EIO;
  424. }
  425. ctx->read_buf = buf;
  426. ctx->read_len = len;
  427. ctx->read_offset = offset;
  428. ctx->read_done_len = 0;
  429. ctx->read_buf_len = 0;
  430. ctx->last_psn = 0xff;
  431. if (!buf) {
  432. ctx->state = PROT_STATE_IDLE;
  433. return 0;
  434. }
  435. read_mask = 0x0;
  436. send_buf = ctx->send_buf + sizeof(struct svc_prot_head);
  437. send_buf = TLV_PACK_U32(send_buf, 0x1, offset);
  438. send_buf = TLV_PACK_U32(send_buf, 0x2, len);
  439. send_buf = TLV_PACK_U8(send_buf, 0x3, read_mask);
  440. send_len = send_buf - ctx->send_buf;
  441. err = ota_bt_send_cmd(ctx, OTA_CMD_D2H_REQUIRE_IMAGE_DATA, ctx->send_buf, send_len);
  442. if (err) {
  443. SYS_LOG_ERR("failed to send cmd %d, error %d",
  444. OTA_CMD_D2H_REQUIRE_IMAGE_DATA, err);
  445. return err;
  446. }
  447. ctx->state = PROT_STATE_IDLE;
  448. return 0;
  449. }
  450. int ota_cmd_h2d_send_image_data(struct svc_prot_context *ctx, uint16_t param_len, int with_crc)
  451. {
  452. int err, seg_len;
  453. uint8_t psn;
  454. uint32_t crc, crc_orig;
  455. SYS_LOG_DBG("buf %p, len %d", ctx->read_buf, ctx->read_len);
  456. if (ctx->read_buf == NULL || ctx->read_len == 0 || param_len < 1) {
  457. svc_drop_all_rx_data(ctx, 20);
  458. return -EINVAL;
  459. }
  460. /* read psn */
  461. err = svc_prot_get_rx_data(ctx, &psn, sizeof(uint8_t));
  462. if (err) {
  463. SYS_LOG_ERR("read error, err %d", err);
  464. return -EIO;
  465. }
  466. param_len -= sizeof(uint8_t);
  467. SYS_LOG_DBG("last_psn %d, cur psn %d\n", ctx->last_psn, psn);
  468. if (psn != (uint8_t)(ctx->last_psn + 1)) {
  469. SYS_LOG_ERR("last_psn %d, cur psn %d not seq\n", ctx->last_psn, psn);
  470. return -EIO;
  471. }
  472. if (with_crc) {
  473. /* read crc field */
  474. err = svc_prot_get_rx_data(ctx, (uint8_t *)&crc_orig, sizeof(uint32_t));
  475. if (err) {
  476. SYS_LOG_ERR("read error, err %d", err);
  477. return -EIO;
  478. }
  479. param_len -= sizeof(uint32_t);
  480. }
  481. seg_len = ctx->read_len - ctx->read_done_len;
  482. if (seg_len > param_len) {
  483. seg_len = param_len;
  484. }
  485. if (ctx->read_buf_len + seg_len > OTA_DATA_BUFFER_SIZE) {
  486. SYS_LOG_ERR("read_buf_len %d + seg_len %d > 4KB.\n", ctx->read_buf_len, seg_len);
  487. return -EIO;
  488. }
  489. /* read data */
  490. err = svc_prot_get_rx_data(ctx, ctx->read_buf + ctx->read_buf_len, seg_len);
  491. if (err) {
  492. SYS_LOG_ERR("read error, err %d", err);
  493. return -EIO;
  494. }
  495. if (with_crc) {
  496. crc = utils_crc32(0, ctx->read_buf + ctx->read_buf_len, seg_len);
  497. if (crc != crc_orig) {
  498. SYS_LOG_ERR("psn%d: crc check error, orig 0x%x != 0x%x",
  499. psn, crc_orig, crc);
  500. return -EIO;
  501. }
  502. }
  503. param_len -= seg_len;
  504. if (param_len != 0) {
  505. SYS_LOG_ERR("unknown remain param, len %d\n", param_len);
  506. return -EIO;
  507. }
  508. ctx->read_done_len += seg_len;
  509. ctx->read_buf_len += seg_len;
  510. ctx->last_psn = psn;
  511. ctx->state = PROT_STATE_IDLE;
  512. return 0;
  513. }
  514. int ota_cmd_h2d_send_image_data_with_crc(struct svc_prot_context *ctx, uint16_t param_len)
  515. {
  516. return ota_cmd_h2d_send_image_data(ctx, param_len, 1);
  517. }
  518. int ota_cmd_h2d_send_image_data_no_crc(struct svc_prot_context *ctx, uint16_t param_len)
  519. {
  520. return ota_cmd_h2d_send_image_data(ctx, param_len, 0);
  521. }
  522. int ota_cmd_d2h_report_image_valid(struct svc_prot_context *ctx, int is_valid)
  523. {
  524. uint8_t *send_buf;
  525. uint8_t valid_flag;
  526. int err, send_len;
  527. if (!ctx->negotiation_done) {
  528. SYS_LOG_ERR("negotiation not done");
  529. return -EIO;
  530. }
  531. valid_flag = is_valid ? 1 : 0;
  532. send_buf = ctx->send_buf + sizeof(struct svc_prot_head);
  533. send_buf = TLV_PACK_U8(send_buf, 0x1, valid_flag);
  534. send_len = send_buf - ctx->send_buf;
  535. err = ota_bt_send_cmd(ctx, OTA_CMD_D2H_VALIDATE_IMAGE, ctx->send_buf, send_len);
  536. if (err) {
  537. SYS_LOG_ERR("failed to send cmd %d, error %d",
  538. OTA_CMD_D2H_VALIDATE_IMAGE, err);
  539. }
  540. /* reset state to idle anyway */
  541. ctx->state = PROT_STATE_IDLE;
  542. return err;
  543. }
  544. struct svc_prot_cmd svc_cmds[] = {
  545. {OTA_CMD_H2D_REQUEST_UPGRADE, ota_cmd_h2d_request_upgrade,},
  546. {OTA_CMD_H2D_CONNECT_NEGOTIATION, ota_cmd_h2d_connect_negotiation,},
  547. {OTA_CMD_H2D_NEGOTIATION_RESULT, ota_cmd_h2d_negotiation_result,},
  548. //{OTA_CMD_D2H_REQUIRE_IMAGE_DATA, ota_cmd_d2h_require_image_data,},
  549. {OTA_CMD_H2D_SEND_IMAGE_DATA, ota_cmd_h2d_send_image_data_no_crc,},
  550. {OTA_CMD_H2D_SEND_IMAGE_DATA_WITH_CRC, ota_cmd_h2d_send_image_data_with_crc,},
  551. };
  552. int process_command(struct svc_prot_context *ctx, uint32_t *processed_cmd)
  553. {
  554. struct svc_prot_head head;
  555. svc_prot_cmd_handler_t cmd_handler;
  556. int i, err;
  557. if (ctx->state != PROT_STATE_IDLE) {
  558. SYS_LOG_ERR("current state is not idle");
  559. return -EIO;
  560. }
  561. err = svc_prot_get_rx_data(ctx, (uint8_t *)&head, sizeof(struct svc_prot_head));
  562. if (err) {
  563. SYS_LOG_ERR("cannot read head bytes");
  564. return -EIO;
  565. }
  566. SYS_LOG_DBG("svc head: svc_id 0x%x, cmd_id 0x%x, param type 0x%x, len 0x%x",
  567. head.svc_id, head.cmd, head.param_type, head.param_len);
  568. ctx->state = PROT_STATE_DATA;
  569. if (head.svc_id != SERVICE_ID_OTA) {
  570. SYS_LOG_ERR("invalid svc_id: %d", head.svc_id);
  571. return -EIO;
  572. }
  573. if (head.param_type != TLV_TYPE_MAIN) {
  574. SYS_LOG_ERR("invalid param type: %d", head.svc_id);
  575. return -EIO;
  576. }
  577. for (i = 0; i < ARRAY_SIZE(svc_cmds); i++) {
  578. if (svc_cmds[i].cmd == head.cmd) {
  579. cmd_handler = svc_cmds[i].handler;
  580. err = cmd_handler(ctx, head.param_len);
  581. if (processed_cmd){
  582. *processed_cmd = head.cmd;
  583. }
  584. if (err) {
  585. SYS_LOG_ERR("cmd_handler %p, err: %d", cmd_handler, err);
  586. return err;
  587. }
  588. }
  589. }
  590. if (i > ARRAY_SIZE(svc_cmds)) {
  591. SYS_LOG_ERR("invalid cmd: %d", head.cmd);
  592. return -1;
  593. }
  594. SYS_LOG_DBG("after parse ctx %p, state %d", ctx, ctx->state);
  595. ctx->state = PROT_STATE_IDLE;
  596. return err;
  597. }
  598. int wait_negotiation_done(struct svc_prot_context *ctx)
  599. {
  600. int err = 0;
  601. SYS_LOG_INF("wait, ctx %p\n", ctx);
  602. while (ctx->negotiation_done == 0) {
  603. err = process_command(ctx, NULL);
  604. if (err) {
  605. break;
  606. }
  607. }
  608. SYS_LOG_INF("wait ctx %p, return %d\n", ctx, err);
  609. return err;
  610. }
  611. int ota_backend_bt_ioctl(struct ota_backend *backend, int cmd, unsigned int param)
  612. {
  613. struct ota_backend_bt *backend_bt = CONTAINER_OF(backend,
  614. struct ota_backend_bt, backend);
  615. struct svc_prot_context *svc_ctx = &backend_bt->svc_ctx;
  616. int err;
  617. SYS_LOG_INF("cmd 0x%x: param %d\n", cmd, param);
  618. switch (cmd) {
  619. case OTA_BACKEND_IOCTL_REPORT_IMAGE_VALID:
  620. err = ota_cmd_d2h_report_image_valid(svc_ctx, param);
  621. if (err) {
  622. SYS_LOG_INF("send cmd 0x%x error", cmd);
  623. return -EIO;
  624. }
  625. break;
  626. case OTA_BACKEND_IOCTL_REPORT_PROCESS:
  627. backend->cb(backend, OTA_BACKEND_UPGRADE_PROGRESS, param);
  628. break;
  629. case OTA_BACKEND_IOCTL_GET_UNIT_SIZE:
  630. *(unsigned int*)param = svc_ctx->ota_unit_size;
  631. break;
  632. case OTA_BACKEND_IOCTL_GET_MAX_SIZE:
  633. if (svc_ctx->ota_unit_size <= OTA_UNIT_BLE_SIZE) {
  634. *(unsigned int*)param = svc_ctx->ota_unit_size * OTA_IOS_BLE_MAX_PKT;
  635. }
  636. break;
  637. case OTA_BACKEND_IOCTL_GET_CONNECT_TYPE:
  638. *(unsigned int*)param = svc_ctx->connect_type;
  639. break;
  640. case OTA_BACKEND_IOCTL_EXECUTE_EXIT:
  641. backend->cb(backend, OTA_BACKEND_UPGRADE_EXIT, 0);
  642. break;
  643. default:
  644. SYS_LOG_ERR("unknow cmd 0x%x", cmd);
  645. return -EINVAL;
  646. }
  647. return 0;
  648. }
  649. int ota_backend_bt_read(struct ota_backend *backend, int offset, uint8_t *buf, int size)
  650. {
  651. struct ota_backend_bt *backend_bt = CONTAINER_OF(backend,
  652. struct ota_backend_bt, backend);
  653. struct svc_prot_context *svc_ctx = &backend_bt->svc_ctx;
  654. int err, retry_times = 0;
  655. uint32_t processed_cmd;
  656. SYS_LOG_INF("offset 0x%x, size %d, buf %p", offset, size, buf);
  657. try_again:
  658. err = ota_cmd_require_image_data(svc_ctx, offset, size, buf);
  659. if (err) {
  660. SYS_LOG_INF("read data err %d", err);
  661. return -EIO;
  662. }
  663. while (svc_ctx->read_done_len != size) {
  664. err = process_command(svc_ctx, &processed_cmd);
  665. if (err) {
  666. SYS_LOG_INF("retrun err %d", err);
  667. break;
  668. }
  669. if(processed_cmd == OTA_CMD_H2D_NEGOTIATION_RESULT){
  670. offset += svc_ctx->read_done_len;
  671. buf += svc_ctx->read_done_len;
  672. size -= svc_ctx->read_done_len;
  673. goto try_again;
  674. }
  675. SYS_LOG_DBG("read_done_len size %d", svc_ctx->read_done_len);
  676. }
  677. if (err && (retry_times < 1) &&
  678. (NONE_CONNECT_TYPE != svc_ctx->connect_type)) {
  679. /* wait 500ms and drop all data in stream buffer */
  680. svc_drop_all_rx_data(svc_ctx, 500);
  681. svc_ctx->state = PROT_STATE_IDLE;
  682. retry_times++;
  683. SYS_LOG_INF("re-read offset 0x%x, size %d, buf %p", offset, size, buf);
  684. goto try_again;
  685. }
  686. return err;
  687. }
  688. int ota_backend_bt_read_prepare(struct ota_backend *backend, int offset, uint8_t *buf, int size)
  689. {
  690. struct ota_backend_bt *backend_bt = CONTAINER_OF(backend,
  691. struct ota_backend_bt, backend);
  692. struct svc_prot_context *svc_ctx = &backend_bt->svc_ctx;
  693. int err;
  694. SYS_LOG_INF("offset 0x%x, size %d, buf %p", offset, size, buf);
  695. err = ota_cmd_require_image_data(svc_ctx, offset, size, buf);
  696. if (err) {
  697. SYS_LOG_INF("read data err %d", err);
  698. return -EIO;
  699. }
  700. return err;
  701. }
  702. int ota_backend_bt_read_complete(struct ota_backend *backend, int offset, uint8_t *buf, int size)
  703. {
  704. struct ota_backend_bt *backend_bt = CONTAINER_OF(backend,
  705. struct ota_backend_bt, backend);
  706. struct svc_prot_context *svc_ctx = &backend_bt->svc_ctx;
  707. int err = 0, retry_times = 0, wait_ms, retry_size = size;
  708. uint32_t processed_cmd;
  709. SYS_LOG_INF("offset 0x%x, size %d, buf %p", offset, size, buf);
  710. try_again:
  711. if (retry_times > 0) {
  712. err = ota_cmd_require_image_data(svc_ctx, offset, retry_size, buf);
  713. if (err) {
  714. SYS_LOG_INF("read data err %d", err);
  715. return -EIO;
  716. }
  717. }
  718. while (svc_ctx->read_buf_len != size) {
  719. err = process_command(svc_ctx, &processed_cmd);
  720. if (err) {
  721. SYS_LOG_INF("retrun err %d", err);
  722. break;
  723. }
  724. if(processed_cmd == OTA_CMD_H2D_NEGOTIATION_RESULT){
  725. offset += svc_ctx->read_buf_len;
  726. buf += svc_ctx->read_buf_len;
  727. size -= svc_ctx->read_buf_len;
  728. goto try_again;
  729. }
  730. SYS_LOG_DBG("read_buf_len size %d", svc_ctx->read_buf_len);
  731. }
  732. if (err && (retry_times < 1) &&
  733. (NONE_CONNECT_TYPE != svc_ctx->connect_type)) {
  734. /* wait and drop all data in stream buffer */
  735. wait_ms = svc_ctx->read_len / 50 + 500;
  736. svc_drop_all_rx_data(svc_ctx, wait_ms);
  737. svc_ctx->state = PROT_STATE_IDLE;
  738. retry_times++;
  739. retry_size = svc_ctx->read_len - (offset - svc_ctx->read_offset);
  740. SYS_LOG_INF("re-read offset 0x%x, size %d, buf %p", offset, retry_size, buf);
  741. goto try_again;
  742. }
  743. svc_ctx->read_buf_len = 0;
  744. return err;
  745. }
  746. int ota_backend_bt_open(struct ota_backend *backend)
  747. {
  748. struct ota_backend_bt *backend_bt = CONTAINER_OF(backend,
  749. struct ota_backend_bt, backend);
  750. struct svc_prot_context *svc_ctx = &backend_bt->svc_ctx;
  751. int err;
  752. if (svc_ctx->sppble_stream) {
  753. err = stream_open(svc_ctx->sppble_stream, MODE_IN_OUT);
  754. if (err) {
  755. SYS_LOG_ERR("stream_open Failed");
  756. return err;
  757. } else {
  758. svc_ctx->sppble_stream_opened = 1;
  759. }
  760. }
  761. SYS_LOG_INF("open sppble_stream %p", svc_ctx->sppble_stream);
  762. wait_negotiation_done(svc_ctx);
  763. return 0;
  764. }
  765. int ota_backend_bt_close(struct ota_backend *backend)
  766. {
  767. struct ota_backend_bt *backend_bt = CONTAINER_OF(backend,
  768. struct ota_backend_bt, backend);
  769. struct svc_prot_context *svc_ctx = &backend_bt->svc_ctx;
  770. int err;
  771. SYS_LOG_INF("close: type %d", backend->type);
  772. if (svc_ctx->sppble_stream_opened) {
  773. err = stream_close(svc_ctx->sppble_stream);
  774. if (err) {
  775. SYS_LOG_ERR("stream_close Failed");
  776. } else {
  777. svc_ctx->sppble_stream_opened = 0;
  778. }
  779. /* clear internal status */
  780. svc_ctx->negotiation_done = 0;
  781. svc_ctx->state = PROT_STATE_IDLE;
  782. }
  783. return 0;
  784. }
  785. void ota_backend_bt_exit(struct ota_backend *backend)
  786. {
  787. struct ota_backend_bt *backend_bt = CONTAINER_OF(backend,
  788. struct ota_backend_bt, backend);
  789. struct svc_prot_context *svc_ctx = &backend_bt->svc_ctx;
  790. /* avoid connect again after exit */
  791. g_backend_bt = NULL;
  792. if (svc_ctx->sppble_stream) {
  793. stream_destroy(svc_ctx->sppble_stream);
  794. }
  795. if (backend_bt->svc_ctx.send_buf)
  796. mem_free(backend_bt->svc_ctx.send_buf);
  797. mem_free(backend_bt);
  798. }
  799. #ifdef CONFIG_BT_BLE
  800. #ifdef CONFIG_BT_BLE_APP_UPDATE_PARAM
  801. #define BLE_OTA_CHECK_PARAM_INTERVAL 2000 /* 2s */
  802. enum {
  803. BLE_PHONE_TYPE_ANDROID,
  804. BLE_PHONE_TYPE_IOS,
  805. BLE_PHONE_TYPE_MAX,
  806. };
  807. enum {
  808. BLE_OTA_LEVEL_FAST,
  809. BLE_OTA_LEVEL_BUSY,
  810. BLE_OTA_LEVEL_IDLE,
  811. BLE_OTA_LEVEL_MAX,
  812. };
  813. const struct bt_le_conn_param le_ota_param[BLE_PHONE_TYPE_MAX][BLE_OTA_LEVEL_MAX] = {
  814. {
  815. {6,12,0,600},
  816. {36,72,0,600},
  817. {32,48,4,600},
  818. },
  819. {
  820. {12,28,0,600},
  821. {36,72,0,600},
  822. {32,48,4,600},
  823. },
  824. };
  825. void ble_ota_param_set_wakelock(bool set)
  826. {
  827. static uint8_t param_wake_lock = 0;
  828. if (set) {
  829. if (!param_wake_lock) {
  830. param_wake_lock = 1;
  831. ble_ota_wake_lock();
  832. SYS_LOG_INF("Le param lock");
  833. }
  834. } else {
  835. if (param_wake_lock) {
  836. param_wake_lock = 0;
  837. ble_ota_wake_unlock();
  838. SYS_LOG_INF("Le param unlock");
  839. }
  840. }
  841. }
  842. /* Be careful: If have multi ble module need operate update ble parameter, need only operate in one place. */
  843. static void ble_ota_check_param_delaywork(os_work *work)
  844. {
  845. int phone_type, br_busy;
  846. uint16_t curr_int = 0, curr_lat = 0;
  847. uint16_t need_min_int, need_max_int;
  848. uint8_t need_ota_leve = BLE_OTA_LEVEL_IDLE;
  849. SYS_LOG_INF("");
  850. #ifdef CONFIG_GATT_OVER_BREDR
  851. if (!bt_manager_ble_is_connected()) {
  852. return;
  853. }
  854. #endif
  855. phone_type = bt_manager_ble_get_phone_type();
  856. br_busy = bt_manager_ble_is_br_busy();
  857. if (bt_manager_ble_get_param(&curr_int, &curr_lat, NULL)) {
  858. goto next_work;
  859. }
  860. if (curr_int == 0) {
  861. goto next_work;
  862. }
  863. need_ota_leve = br_busy ? BLE_OTA_LEVEL_BUSY : BLE_OTA_LEVEL_FAST;
  864. curr_int += curr_int*curr_lat;
  865. if (phone_type >= BLE_PHONE_TYPE_MAX){
  866. phone_type = BLE_PHONE_TYPE_ANDROID;
  867. }
  868. need_min_int = le_ota_param[phone_type][need_ota_leve].interval_min * (1 + le_ota_param[phone_type][need_ota_leve].latency);
  869. need_max_int = le_ota_param[phone_type][need_ota_leve].interval_max * (1 + le_ota_param[phone_type][need_ota_leve].latency);
  870. SYS_LOG_INF("curr_int %d %d %d", curr_int, need_max_int, need_min_int);
  871. if ((curr_int < need_min_int) || (curr_int > need_max_int)) {
  872. /* Be careful: If failed to update several times, refer to stop update again */
  873. SYS_LOG_INF("App set param %d %d", phone_type, need_ota_leve);
  874. bt_manager_ble_update_param(&le_ota_param[phone_type][need_ota_leve]);
  875. goto next_work;
  876. } else {
  877. if (need_ota_leve != BLE_OTA_LEVEL_IDLE) {
  878. goto next_work;
  879. } else {
  880. /* In BLE_OTA_LEVEL_IDLE and not need update parameter,
  881. * stop delaywork, clear wakelock.
  882. */
  883. SYS_LOG_INF("ble update exit");
  884. ble_ota_param_set_wakelock(false);
  885. return;
  886. }
  887. }
  888. next_work:
  889. os_delayed_work_submit(&ble_ota_uparam_work, BLE_OTA_CHECK_PARAM_INTERVAL);
  890. }
  891. #endif
  892. #endif
  893. static void sppble_connect_cb(int connected, uint8_t connect_type)
  894. {
  895. struct ota_backend *backend;
  896. uint16_t mtu;
  897. SYS_LOG_INF("connect: %d connect_type %d.", connected, connect_type);
  898. /* avoid connect again after exit */
  899. if (g_backend_bt) {
  900. backend = &g_backend_bt->backend;
  901. if (backend->cb) {
  902. SYS_LOG_INF("call cb %p", backend->cb);
  903. if (connected && (SPP_CONNECT_TYPE == connect_type)) {
  904. g_backend_bt->svc_ctx.ota_unit_size = OTA_UNIT_FRCOMM_SIZE;
  905. } else if (connected && (BLE_CONNECT_TYPE == connect_type)) {
  906. mtu = bt_manager_get_ble_mtu();
  907. if ((mtu > OTA_UNIT_BLE_SIZE) || (mtu < 100)) {
  908. g_backend_bt->svc_ctx.ota_unit_size = OTA_UNIT_BLE_SIZE;
  909. } else {
  910. g_backend_bt->svc_ctx.ota_unit_size = mtu - 15;
  911. }
  912. #ifdef CONFIG_OTA_GATT_OVER_EDR_TEST
  913. #define OTA_UNIT_EDR_SIZE (323)
  914. if (!bt_manager_ble_is_connected()) {
  915. g_backend_bt->svc_ctx.ota_unit_size = OTA_UNIT_EDR_SIZE;
  916. }
  917. #endif
  918. } else {
  919. g_backend_bt->svc_ctx.ota_unit_size = OTA_UNIT_SIZE;
  920. }
  921. #ifdef CONFIG_BT_BLE
  922. #ifdef CONFIG_BT_BLE_APP_UPDATE_PARAM
  923. if (BLE_CONNECT_TYPE == connect_type) {
  924. if (connected) {
  925. os_delayed_work_submit(&ble_ota_uparam_work, BLE_OTA_CHECK_PARAM_INTERVAL);
  926. ble_ota_param_set_wakelock(true);
  927. } else {
  928. os_delayed_work_cancel(&ble_ota_uparam_work);
  929. ble_ota_param_set_wakelock(false);
  930. }
  931. }
  932. #endif
  933. #endif
  934. if (!connected) {
  935. g_backend_bt->svc_ctx.connect_type = NONE_CONNECT_TYPE;
  936. } else {
  937. g_backend_bt->svc_ctx.connect_type = connect_type;
  938. }
  939. backend->cb(backend, OTA_BACKEND_UPGRADE_STATE, connected);
  940. }
  941. }
  942. }
  943. static struct ota_backend_api ota_backend_api_bt = {
  944. /* .init = ota_backend_bt_init, */
  945. .exit = ota_backend_bt_exit,
  946. .open = ota_backend_bt_open,
  947. .close = ota_backend_bt_close,
  948. .read = ota_backend_bt_read,
  949. .ioctl = ota_backend_bt_ioctl,
  950. .read_prepare = ota_backend_bt_read_prepare,
  951. .read_complete = ota_backend_bt_read_complete,
  952. };
  953. struct ota_backend *ota_backend_bt_init(ota_backend_notify_cb_t cb,
  954. struct ota_backend_bt_init_param *param)
  955. {
  956. struct ota_backend_bt *backend_bt;
  957. struct svc_prot_context *svc_ctx;
  958. struct sppble_stream_init_param init_param;
  959. SYS_LOG_INF("init");
  960. backend_bt = mem_malloc(sizeof(struct ota_backend_bt));
  961. if (!backend_bt) {
  962. SYS_LOG_ERR("malloc failed");
  963. return NULL;
  964. }
  965. memset(backend_bt, 0x0, sizeof(struct ota_backend_bt));
  966. svc_ctx = &backend_bt->svc_ctx;
  967. memset(&init_param, 0, sizeof(struct sppble_stream_init_param));
  968. init_param.spp_uuid = (uint8_t *)param->spp_uuid;
  969. init_param.gatt_attr = param->gatt_attr;
  970. init_param.attr_size = param->attr_size;
  971. init_param.tx_chrc_attr = param->tx_chrc_attr;
  972. init_param.tx_attr = param->tx_attr;
  973. init_param.tx_ccc_attr = param->tx_ccc_attr;
  974. init_param.rx_attr = param->rx_attr;
  975. init_param.connect_cb = sppble_connect_cb;
  976. init_param.read_timeout = param->read_timeout; /* K_FOREVER, K_NO_WAIT, K_MSEC(ms) */
  977. init_param.write_timeout = param->write_timeout;
  978. init_param.read_buf_size = OTA_SPPBLE_BUFF_SIZE;
  979. /* Just call stream_create once, for register spp/ble service
  980. * not need call stream_destroy
  981. */
  982. svc_ctx->sppble_stream = sppble_stream_create(&init_param);
  983. if (!svc_ctx->sppble_stream) {
  984. SYS_LOG_ERR("stream_create failed");
  985. }
  986. svc_ctx->send_buf = mem_malloc(OTA_SVC_SEND_BUFFER_SIZE);
  987. svc_ctx->send_buf_size = OTA_SVC_SEND_BUFFER_SIZE;
  988. svc_ctx->ota_unit_size = OTA_UNIT_SIZE;
  989. svc_ctx->state = PROT_STATE_IDLE;
  990. g_backend_bt = backend_bt;
  991. ota_backend_init(&backend_bt->backend, OTA_BACKEND_TYPE_BLUETOOTH,
  992. &ota_backend_api_bt, cb);
  993. #ifdef CONFIG_BT_BLE
  994. #ifdef CONFIG_BT_BLE_APP_UPDATE_PARAM
  995. os_delayed_work_init(&ble_ota_uparam_work, ble_ota_check_param_delaywork);
  996. #endif
  997. #endif
  998. return &backend_bt->backend;
  999. }
  1000. struct ota_backend *ota_backend_load_bt_init(ota_backend_notify_cb_t cb,
  1001. struct ota_backend_bt_init_param *param, stream_cb scb, io_stream_t *pexist_stream)
  1002. {
  1003. struct ota_backend_bt *backend_bt;
  1004. struct svc_prot_context *svc_ctx;
  1005. struct sppble_stream_init_param init_param;
  1006. SYS_LOG_INF("init");
  1007. if (!g_backend_bt)
  1008. {
  1009. backend_bt = mem_malloc(sizeof(struct ota_backend_bt));
  1010. if (!backend_bt) {
  1011. SYS_LOG_ERR("malloc failed");
  1012. return NULL;
  1013. }
  1014. }
  1015. else
  1016. {
  1017. backend_bt = g_backend_bt;
  1018. }
  1019. memset(backend_bt, 0x0, sizeof(struct ota_backend_bt));
  1020. svc_ctx = &backend_bt->svc_ctx;
  1021. memset(&init_param, 0, sizeof(struct sppble_stream_init_param));
  1022. init_param.spp_uuid = (uint8_t *)param->spp_uuid;
  1023. init_param.gatt_attr = param->gatt_attr;
  1024. init_param.attr_size = param->attr_size;
  1025. init_param.tx_chrc_attr = param->tx_chrc_attr;
  1026. init_param.tx_attr = param->tx_attr;
  1027. init_param.tx_ccc_attr = param->tx_ccc_attr;
  1028. init_param.rx_attr = param->rx_attr;
  1029. init_param.connect_cb = sppble_connect_cb;
  1030. init_param.read_timeout = param->read_timeout; /* K_FOREVER, K_NO_WAIT, K_MSEC(ms) */
  1031. init_param.write_timeout = param->write_timeout;
  1032. init_param.read_buf_size = OTA_SPPBLE_BUFF_SIZE;
  1033. /* Just call stream_create once, for register spp/ble service
  1034. * not need call stream_destroy
  1035. */
  1036. svc_ctx->sppble_stream = scb(&init_param);
  1037. if (!svc_ctx->sppble_stream) {
  1038. SYS_LOG_ERR("stream_create failed");
  1039. }
  1040. if (!svc_ctx->send_buf)
  1041. svc_ctx->send_buf = mem_malloc(OTA_SVC_SEND_BUFFER_SIZE);
  1042. svc_ctx->send_buf_size = OTA_SVC_SEND_BUFFER_SIZE;
  1043. svc_ctx->ota_unit_size = OTA_UNIT_SIZE;
  1044. svc_ctx->state = PROT_STATE_IDLE;
  1045. g_backend_bt = backend_bt;
  1046. ota_backend_init(&backend_bt->backend, OTA_BACKEND_TYPE_BLUETOOTH,
  1047. &ota_backend_api_bt, cb);
  1048. *pexist_stream = svc_ctx->sppble_stream;
  1049. return &backend_bt->backend;
  1050. }
  1051. void ota_backend_stream_set(io_stream_t exist_stream)
  1052. {
  1053. struct ota_backend_bt *backend_bt;
  1054. struct svc_prot_context *svc_ctx;
  1055. if (!g_backend_bt)
  1056. {
  1057. SYS_LOG_ERR("");
  1058. return;
  1059. }
  1060. backend_bt = g_backend_bt;
  1061. svc_ctx = &backend_bt->svc_ctx;
  1062. svc_ctx->sppble_stream = exist_stream;
  1063. return;
  1064. }
  1065. void ota_backend_ota_type_cb_set(ota_backend_type_cb_t tcb)
  1066. {
  1067. struct ota_backend_bt *backend_bt;
  1068. struct svc_prot_context *svc_ctx;
  1069. if (!g_backend_bt)
  1070. {
  1071. SYS_LOG_ERR("");
  1072. return;
  1073. }
  1074. backend_bt = g_backend_bt;
  1075. svc_ctx = &backend_bt->svc_ctx;
  1076. svc_ctx->ota_type_cb = tcb;
  1077. return;
  1078. }