• T31开发笔记:OTA升级


    若该文为原创文章,转载请注明原文出处

    一、前言

    在嵌入式设备中,升级固件基本是通用它的功能,通过OTA方式实现固件或软件的升级。

    通过无线通信方式实现升级的,都可以叫OTA升级,比如网络/蓝牙。

    通过有线方式进行升级,叫本地升级,比如通过UART,USB或者SPI通信接口来升级设备固件。

    1、下载方式

    不管采用OTA方式还是有线通信方式升级,下载升级包的方式包括后台式下载和非后台式下载两种模式。

    2、新旧固件覆盖模式:

    新固件替换老固件覆盖的两种方式:双区模式和单区模式。

    后台式下载必须采用双区模式进行升级。具体详细不在多描述。只是个概念。

    此文记录通过HTTPS方式下载文件,并替换旧程序的方式达到远程升级功能,需要自行建立HTTPS服务端。

    二、交叉编译openssl

    一、硬件和开发环境


    1、硬件:T31X+SC5235 

    2、开发环境: ubuntu16.04-64bit

    3、编译器:mips-gcc540-glibc222-32bit-r3.3.0.tar.gz

    注:板子和和WIFI模块是某淘上淘的,使用的是RTL8188,使用的是USB接口,uboot和内核是自己裁剪移植的,内核默认自带WIFI驱动,所以不用移植可以直接使用。

    二、交叉编译

    1. export CC=/opt/mips-gcc540-glibc222-32bit-r3.3.0/bin/mips-linux-gnu-gcc
    2. ./Configure linux-mips32
    3. make
    4. make install
    5. 生成的库在/usr/local/lib/目录下
    6. 值得注意的是,在arm交叉编译环境中,引用库的顺序为:-lssl -lcrypto,如果为 -lcrypto -lssl就会编译错误。

    编译成功,把lib文件拷贝到开发板上的usr/lib目录下。

    三、编写HTTPS客户端代码

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <string.h>
    4. #include <sys/types.h>
    5. #include <sys/socket.h>
    6. #include <errno.h>
    7. #include <unistd.h>
    8. #include <netinet/in.h>
    9. #include <limits.h>
    10. #include <netdb.h>
    11. #include <arpa/inet.h>
    12. #include <ctype.h>
    13. #include <openssl/crypto.h>
    14. #include <openssl/ssl.h>
    15. #include <openssl/err.h>
    16. #include <openssl/rand.h>
    17. #define DEBUG 1
    18. /********************************************
    19. 功能:搜索字符串右边起的第一个匹配字符
    20. ********************************************/
    21. char *Rstrchr(char *s, char x)
    22. {
    23. int i = strlen(s);
    24. if (!(*s))
    25. return 0;
    26. while (s[i - 1])
    27. if (strchr(s + (i - 1), x))
    28. return (s + (i - 1));
    29. else
    30. i--;
    31. return 0;
    32. }
    33. /**************************************************************
    34. 功能:从字符串src中分析出网站地址和端口,并得到用户要下载的文件
    35. ***************************************************************/
    36. void GetHost(char *src, char *web, char *file, int *port)
    37. {
    38. char *pA;
    39. char *pB;
    40. memset(web, 0, sizeof(web));
    41. memset(file, 0, sizeof(file));
    42. *port = 0;
    43. if (!(*src))
    44. return;
    45. pA = src;
    46. if (!strncmp(pA, "http://", strlen("http://")))
    47. pA = src + strlen("http://");
    48. else if (!strncmp(pA, "https://", strlen("https://")))
    49. pA = src + strlen("https://");
    50. pB = strchr(pA, '/');
    51. if (pB) {
    52. memcpy(web, pA, strlen(pA) - strlen(pB));
    53. if (pB + 1) {
    54. memcpy(file, pB + 1, strlen(pB) - 1);
    55. file[strlen(pB) - 1] = 0;
    56. }
    57. } else
    58. memcpy(web, pA, strlen(pA));
    59. if (pB)
    60. web[strlen(pA) - strlen(pB)] = 0;
    61. else
    62. web[strlen(pA)] = 0;
    63. pA = strchr(web, ':');
    64. if (pA)
    65. *port = atoi(pA + 1);
    66. else
    67. *port = 443;
    68. }
    69. int main(int argc, char *argv[])
    70. {
    71. int sockfd, ret;
    72. char buffer[1024];
    73. struct sockaddr_in server_addr;
    74. struct hostent *host;
    75. int portnumber, nbytes;
    76. char host_addr[256];
    77. char host_file[1024];
    78. char local_file[256];
    79. FILE *fp;
    80. char request[1024];
    81. int send, totalsend;
    82. int i;
    83. char *pt;
    84. SSL *ssl;
    85. SSL_CTX *ctx;
    86. if (argc != 2) {
    87. if (DEBUG)
    88. fprintf(stderr, "Usage:%s webpage-address\a\n", argv[0]);
    89. exit(1);
    90. }
    91. if (DEBUG)
    92. printf("parameter.1 is: %s\n", argv[1]);
    93. GetHost(argv[1], host_addr, host_file, &portnumber); /*分析网址、端口、文件名等 */
    94. if (DEBUG)
    95. printf("webhost:%s\n", host_addr);
    96. if (DEBUG)
    97. printf("hostfile:%s\n", host_file);
    98. if (DEBUG)
    99. printf("portnumber:%d\n\n", portnumber);
    100. if ((host = gethostbyname(host_addr)) == NULL) { /*取得主机IP地址 */
    101. if (DEBUG)
    102. fprintf(stderr, "Gethostname error, %s\n", strerror(errno));
    103. exit(1);
    104. }
    105. /* 客户程序开始建立 sockfd描述符 */
    106. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { /*建立SOCKET连接 */
    107. if (DEBUG)
    108. fprintf(stderr, "Socket Error:%s\a\n", strerror(errno));
    109. exit(1);
    110. }
    111. /* 客户程序填充服务端的资料 */
    112. bzero(&server_addr, sizeof(server_addr));
    113. server_addr.sin_family = AF_INET;
    114. server_addr.sin_port = htons(portnumber);
    115. server_addr.sin_addr = *((struct in_addr *) host->h_addr);
    116. /* 客户程序发起连接请求 */
    117. if (connect(sockfd, (struct sockaddr *) (&server_addr), sizeof(struct sockaddr)) == -1) { /*连接网站 */
    118. if (DEBUG)
    119. fprintf(stderr, "Connect Error:%s\a\n", strerror(errno));
    120. exit(1);
    121. }
    122. /* SSL初始化 */
    123. SSL_library_init();
    124. SSL_load_error_strings();
    125. ctx = SSL_CTX_new(SSLv23_client_method());
    126. if (ctx == NULL) {
    127. ERR_print_errors_fp(stderr);
    128. exit(1);
    129. }
    130. ssl = SSL_new(ctx);
    131. if (ssl == NULL) {
    132. ERR_print_errors_fp(stderr);
    133. exit(1);
    134. }
    135. /* 把socket和SSL关联 */
    136. ret = SSL_set_fd(ssl, sockfd);
    137. if (ret == 0) {
    138. ERR_print_errors_fp(stderr);
    139. exit(1);
    140. }
    141. RAND_poll();
    142. while (RAND_status() == 0) {
    143. unsigned short rand_ret = rand() % 65536;
    144. RAND_seed(&rand_ret, sizeof(rand_ret));
    145. }
    146. ret = SSL_connect(ssl);
    147. if (ret != 1) {
    148. ERR_print_errors_fp(stderr);
    149. exit(1);
    150. }
    151. sprintf(request, "GET /%s HTTP/1.1\r\nAccept: */*\r\nAccept-Language: zh-cn\r\n\
    152. User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\n\
    153. Host: %s:%d\r\nConnection: Close\r\n\r\n", host_file, host_addr,
    154. portnumber);
    155. if (DEBUG)
    156. printf("%s", request); /*准备request,将要发送给主机 */
    157. /*取得真实的文件名 */
    158. if (host_file && *host_file)
    159. pt = Rstrchr(host_file, '/');
    160. else
    161. pt = 0;
    162. memset(local_file, 0, sizeof(local_file));
    163. if (pt && *pt) {
    164. if ((pt + 1) && *(pt + 1))
    165. strcpy(local_file, pt + 1);
    166. else
    167. memcpy(local_file, host_file, strlen(host_file) - 1);
    168. } else if (host_file && *host_file)
    169. strcpy(local_file, host_file);
    170. else
    171. strcpy(local_file, "index.html");
    172. if (DEBUG)
    173. printf("local filename to write:%s\n\n", local_file);
    174. /*发送https请求request */
    175. send = 0;
    176. totalsend = 0;
    177. nbytes = strlen(request);
    178. while (totalsend < nbytes) {
    179. send = SSL_write(ssl, request + totalsend, nbytes - totalsend);
    180. if (send == -1) {
    181. if (DEBUG)
    182. ERR_print_errors_fp(stderr);
    183. exit(0);
    184. }
    185. totalsend += send;
    186. if (DEBUG)
    187. printf("%d bytes send OK!\n", totalsend);
    188. }
    189. fp = fopen(local_file, "a");
    190. if (!fp) {
    191. if (DEBUG)
    192. printf("create file error! %s\n", strerror(errno));
    193. return 0;
    194. }
    195. if (DEBUG)
    196. printf("\nThe following is the response header:\n");
    197. i = 0;
    198. /* 连接成功了,接收https响应,response */
    199. while ((nbytes = SSL_read(ssl, buffer, 1)) == 1) {
    200. if (i < 4) {
    201. if (buffer[0] == '\r' || buffer[0] == '\n')
    202. i++;
    203. else
    204. i = 0;
    205. if (DEBUG)
    206. printf("%c", buffer[0]); /*把https头信息打印在屏幕上 */
    207. } else {
    208. fwrite(buffer, 1, 1, fp); /*将https主体信息写入文件 */
    209. i++;
    210. if (i % 1024 == 0)
    211. fflush(fp); /*1K时存盘一次 */
    212. }
    213. }
    214. fclose(fp);
    215. /* 结束通讯 */
    216. ret = SSL_shutdown(ssl);
    217. if (ret != 1) {
    218. ERR_print_errors_fp(stderr);
    219. exit(1);
    220. }
    221. close(sockfd);
    222. SSL_free(ssl);
    223. SSL_CTX_free(ctx);
    224. ERR_free_strings();
    225. exit(0);
    226. }

    编译:

    gcc -Wall https-client.c -I/usr/include -L/usr/lib -lssl -o httpsclient
    

    测试:

    ./httpsclient https://192.168.0.109/test.mp3

    服务器是用nginx搭建的,把test.mp3文件放到www目录下。

    程序关键是释放SSL资源。

    四、总结

    1、使用HTTPS方式,服务器建立HTTPS服务端,设备为客户端发起OTA升级是比较常见的一种方法。

    如有侵权,请及时联系博主删除。

  • 相关阅读:
    vcpkg C++依赖包管理工具的基本使用
    SLAM从入门到精通(代码调试)
    Java 设计模式——状态模式
    Spring Aop 入门与理解
    小学生python游戏开发pygame--设置内容整理
    洛谷 P1443 马的遍历 Java
    汽车座舱的“算力赛”升级,移远通信推出AG855G智能模组
    GLIBC中的Symbol Versioning
    HCIA-HarmonyOS Application Developer 课程大纲
    Python统计学01——数据可视化
  • 原文地址:https://blog.csdn.net/weixin_38807927/article/details/128078705