blob: 5ac83c92c1b233fafa290da7f1887e6bcd2829c8 [file] [log] [blame]
b.liu87afc4c2024-08-14 17:33:45 +08001/*
2* main.c
3*
4* MBTK important service support.
5*
6*/
7/******************************************************************************
8
9 EDIT HISTORY FOR FILE
10
11 WHEN WHO WHAT,WHERE,WHY
12-------- -------- -------------------------------------------------------
132024/6/12 LiuBin Initial version
14
15******************************************************************************/
16#include <stdio.h>
b.liudeb8e422024-12-14 17:36:56 +080017#include <stdint.h>
b.liu87afc4c2024-08-14 17:33:45 +080018#include <stdlib.h>
19#include <unistd.h>
20#include <errno.h>
21#include <fcntl.h>
22
23#include "mbtk_type.h"
24#include "mbtk_log.h"
25#include "instance_info.h"
b.liu62240ee2024-11-07 17:52:45 +080026#include "mbtk_str.h"
27#include "mbtk_utils.h"
r.xiaoab6f4ec2024-12-19 03:42:17 -080028#include "mbtk_led.h"
yq.wang6c876f52025-05-17 16:56:50 +080029#include "reboot_reason.h"
b.liu87afc4c2024-08-14 17:33:45 +080030
31#define MBTK_SERVICES_PID_FILE "/var/run/mbtk_servicesd.pid"
32#define MBTK_SERVICES_CONF_FILE "/etc/mbtk_servicesd.conf"
33
34extern int instance_info_size;
35extern instance_info_t instance_infos[];
36int ins_monitor_service_start();
37
38static void config_parse(const char *conf_file)
39{
40 FILE *fptr = fopen(conf_file, "r");
41 if(fptr != NULL) {
42 char line[1024] = {0};
43 bool respawn_process = FALSE;
44 while(fgets(line, sizeof(line), fptr) != NULL && strlen(line) > 0) {
45 if(str_startwith(line, "#")) {
46 memset(line, 0, sizeof(line));
47 continue;
48 }
49
50 if(!respawn_process && str_startwith(line, "respawn_start")) {
51 respawn_process = TRUE;
52 memset(line, 0, sizeof(line));
53 continue;
54 } else if(respawn_process && str_startwith(line, "respawn_end")) {
55 respawn_process = FALSE;
56 memset(line, 0, sizeof(line));
57 continue;
58 }
59
60 char *ptr = line + strlen(line) - 1;
61 while(ptr >= line && (*ptr == '\r' || *ptr == '\n' || *ptr == ' ')) {
62 *ptr-- = '\0';
63 }
64
65 if(ptr < line) { // Empty line
66 memset(line, 0, sizeof(line));
67 continue;
68 }
69
70 if(respawn_process && instance_info_size < INSTANCE_NUM_MAX) {
71 ptr = strstr(line, ":");
72 if(ptr) {
73 *ptr++ = '\0';
74 memcpy(instance_infos[instance_info_size].ins_name, line, strlen(line));
75 memcpy(instance_infos[instance_info_size].ins_cmd, ptr, strlen(ptr));
76 instance_infos[instance_info_size].ins_pid = -1;
77 instance_info_size++;
78 }
79 }
80
81 memset(line, 0, sizeof(line));
82 }
83 fclose(fptr);
84 }
85}
86
87int main(int argc, char *argv[])
88{
89 mbtk_log_init("radio", "MBTK_SERVICES");
90
b.liubcf86c92024-08-19 19:48:28 +080091 MBTK_SOURCE_INFO_PRINT("mbtk_services");
92
b.liu87afc4c2024-08-14 17:33:45 +080093#ifdef MBTK_DUMP_SUPPORT
94 mbtk_debug_open(NULL, TRUE);
95#endif
96
97 if(app_already_running(MBTK_SERVICES_PID_FILE)) {
98 LOGW("daemon already running.");
99 exit(1);
100 }
101
102 config_parse(MBTK_SERVICES_CONF_FILE);
103
104 // Start services.
105 if(ins_monitor_service_start()) {
106 LOGW("ins_monitor_service_start() fail.");
107 }
108
yq.wang6c876f52025-05-17 16:56:50 +0800109 reboot_reason_init();
110
r.xiaoab6f4ec2024-12-19 03:42:17 -0800111#ifdef MBTK_LED
112 //led services
113 if(mbtk_led_init())
114 {
115 LOGW("LED_service_start() fail.");
116 }
117
118 if(mbtk_led_contril_server_init())
119 {
120 LOGW("LED_contril_service_start() fail.");
121 }
122#endif
123
b.liu87afc4c2024-08-14 17:33:45 +0800124 while(1) {
125 sleep(24 * 60 * 60);
126 }
127
128 return 0;
129}
130