blob: 171906eb7fe48c835ebcf8302127bdc689b8a7cd [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"
b.liu87afc4c2024-08-14 17:33:45 +080029
30#define MBTK_SERVICES_PID_FILE "/var/run/mbtk_servicesd.pid"
31#define MBTK_SERVICES_CONF_FILE "/etc/mbtk_servicesd.conf"
32
33extern int instance_info_size;
34extern instance_info_t instance_infos[];
35int ins_monitor_service_start();
36
37static void config_parse(const char *conf_file)
38{
39 FILE *fptr = fopen(conf_file, "r");
40 if(fptr != NULL) {
41 char line[1024] = {0};
42 bool respawn_process = FALSE;
43 while(fgets(line, sizeof(line), fptr) != NULL && strlen(line) > 0) {
44 if(str_startwith(line, "#")) {
45 memset(line, 0, sizeof(line));
46 continue;
47 }
48
49 if(!respawn_process && str_startwith(line, "respawn_start")) {
50 respawn_process = TRUE;
51 memset(line, 0, sizeof(line));
52 continue;
53 } else if(respawn_process && str_startwith(line, "respawn_end")) {
54 respawn_process = FALSE;
55 memset(line, 0, sizeof(line));
56 continue;
57 }
58
59 char *ptr = line + strlen(line) - 1;
60 while(ptr >= line && (*ptr == '\r' || *ptr == '\n' || *ptr == ' ')) {
61 *ptr-- = '\0';
62 }
63
64 if(ptr < line) { // Empty line
65 memset(line, 0, sizeof(line));
66 continue;
67 }
68
69 if(respawn_process && instance_info_size < INSTANCE_NUM_MAX) {
70 ptr = strstr(line, ":");
71 if(ptr) {
72 *ptr++ = '\0';
73 memcpy(instance_infos[instance_info_size].ins_name, line, strlen(line));
74 memcpy(instance_infos[instance_info_size].ins_cmd, ptr, strlen(ptr));
75 instance_infos[instance_info_size].ins_pid = -1;
76 instance_info_size++;
77 }
78 }
79
80 memset(line, 0, sizeof(line));
81 }
82 fclose(fptr);
83 }
84}
85
86int main(int argc, char *argv[])
87{
88 mbtk_log_init("radio", "MBTK_SERVICES");
89
b.liubcf86c92024-08-19 19:48:28 +080090 MBTK_SOURCE_INFO_PRINT("mbtk_services");
91
b.liu87afc4c2024-08-14 17:33:45 +080092#ifdef MBTK_DUMP_SUPPORT
93 mbtk_debug_open(NULL, TRUE);
94#endif
95
96 if(app_already_running(MBTK_SERVICES_PID_FILE)) {
97 LOGW("daemon already running.");
98 exit(1);
99 }
100
101 config_parse(MBTK_SERVICES_CONF_FILE);
102
103 // Start services.
104 if(ins_monitor_service_start()) {
105 LOGW("ins_monitor_service_start() fail.");
106 }
107
r.xiaoab6f4ec2024-12-19 03:42:17 -0800108#ifdef MBTK_LED
109 //led services
110 if(mbtk_led_init())
111 {
112 LOGW("LED_service_start() fail.");
113 }
114
115 if(mbtk_led_contril_server_init())
116 {
117 LOGW("LED_contril_service_start() fail.");
118 }
119#endif
120
b.liu87afc4c2024-08-14 17:33:45 +0800121 while(1) {
122 sleep(24 * 60 * 60);
123 }
124
125 return 0;
126}
127