blob: 110a97cb1968d21a504ad9f9bdfccc37cb79e89d [file] [log] [blame]
lichengzhang45091302023-10-08 02:10:34 -07001#include <string.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <error.h>
5#include <string.h>
6#include <errno.h>
7#include <liblog/lynq_deflog.h>
8#include "include/liblynq-at-common.h"
jb.qi539afe42023-11-23 01:52:18 -08009#include <include/lynq-qser-autosuspend.h>
rita6966c302024-01-09 10:50:18 +080010#include <libled/lynq_led.h>
lichengzhang45091302023-10-08 02:10:34 -070011
12DEFINE_LYNQ_LIB_LOG(LYNQ_AT_COMMON)
13
14lynq_atsvc_outcb handle_output;
15typedef struct
16{
17 char *cmd;
18 void (*func)(char *input);
19}Command;
20
21enum
22{
23 Response = 0,
24 Urc
25};
26
27void lynq_response_ok()
28{
lichengzhanga252bb82023-10-14 00:22:02 -070029 char *str = "OK\r\n";
lichengzhang45091302023-10-08 02:10:34 -070030 handle_output(str, strlen(str), Response);
31}
32
33void lynq_response_error(int error_code)
34{
35 char str[32] = {0};
lichengzhanga252bb82023-10-14 00:22:02 -070036 sprintf(str, "+CME ERROR: %d\r\n", error_code);
lichengzhang45091302023-10-08 02:10:34 -070037 handle_output(str, strlen(str), Response);
38}
39
40void lynq_handle_version()
41{
42 char buf[64] = {0};
lichengzhanga252bb82023-10-14 00:22:02 -070043 sprintf(buf,"+CGIR:%s\r\n",LYNQ_SW_INSIDE_VERSION);
lichengzhang45091302023-10-08 02:10:34 -070044 handle_output(buf, strlen(buf), Response);
45 lynq_response_ok();
46 return;
47}
48
jb.qi539afe42023-11-23 01:52:18 -080049void lynq_handle_autosuspend(char* input)
50{
51 int len;
52 int ret;
53 char buf[64] = {0};
54 ALOGE("lynq_handle_autosuspend start\n");
55 len = strlen(input);
jb.qi3763a822023-12-21 02:05:12 -080056 system("echo 0 >/sys/devices/platform/soc/1307000.gmac/gmac_power");
jb.qi539afe42023-11-23 01:52:18 -080057 ret = qser_autosuspend_enable(input[len-1]);
58 if(ret != 0)
59 {
60 sprintf(buf,"+CME ERROR: 100\r\n");
61 handle_output(buf, strlen(buf), Response);
62 }
63 else
64 {
65 lynq_response_ok();
66 }
67
68 return;
69}
70
rita6966c302024-01-09 10:50:18 +080071void lynq_handle_netled(char* input)
72{
73 int ret;
74 char buf[64] = {0};
75 int mode = input[strlen(input)-1]-'0';
76 ALOGE("lynq_handle_netled start\n");
77
78 ret = lynq_set_netled_on(mode);
79 if(ret != 0)
80 {
81 sprintf(buf,"+CME ERROR: 100\r\n");
82 handle_output(buf, strlen(buf), Response);
83 }
84 else
85 {
86 lynq_response_ok();
87 }
88
89 return;
90}
91
92void lynq_handle_statusled(char* input)
93{
94 int ret;
95 char buf[64] = {0};
96 int mode = input[strlen(input)-1]-'0';
97
98 ALOGE("lynq_handle_statusled start\n");
99 ret = lynq_set_statusled_on(mode);
100
101 if(ret != 0)
102 {
103 sprintf(buf,"+CME ERROR: 100\r\n");
104 handle_output(buf, strlen(buf), Response);
105 }
106 else
107 {
108 lynq_response_ok();
109 }
110
111 return;
112}
lichengzhang136c1842024-04-17 16:59:03 +0800113#define mem_addr_lenth 10
114void lynq_handle_ddr_identify()
115{
116 char buf[64] = {0};
117 char *command = "devmem 0x0121b040";
118 char result[64] = {0};
119 FILE *fp;
120
121 fp = popen(command, "r");
122 if(fp == NULL)
123 {
124 lynq_response_error(100);
125 return;
126 }
127 fgets(result, sizeof(result), fp);
128 pclose(fp);
129
130 if(strncmp(result,"0xF8631600",mem_addr_lenth) == 0 || strncmp(result,"0xF8631700",mem_addr_lenth) == 0)
131 {
132 sprintf(buf,"+DDRID:2Gb ddr\r\n");
133 handle_output(buf, strlen(buf), Response);
134 }
135 else if(strncmp(result,"0xF8631300",mem_addr_lenth) == 0 || strncmp(result,"0xF8631500",mem_addr_lenth) == 0)
136 {
137 sprintf(buf,"+DDRID:4Gb ddr\r\n");
138 handle_output(buf, strlen(buf), Response);
139 }
140 else
141 {
142 sprintf(buf,"+DDRID:no matching ddr\r\n");
143 handle_output(buf, strlen(buf), Response);
144 lynq_response_error(100);
145 return;
146 }
147 lynq_response_ok();
148 return;
149}
150
lichengzhang51d33132024-10-17 14:36:42 +0800151void lynq_handle_halt_enable()
152{
153 int ret;
154 ret = system("echo 0x7:0x0 >/sys/kernel/debug/pmu_zx29/regs");
155 if(ret != 0)
156 {
157 lynq_response_error(100);
158 return;
159 }
160 lynq_response_ok();
161 return;
162}
163
jb.qi539afe42023-11-23 01:52:18 -0800164static Command commands[] =
lichengzhang45091302023-10-08 02:10:34 -0700165{
166 {"CGIR",lynq_handle_version},
jb.qi539afe42023-11-23 01:52:18 -0800167 {"LEELSP",lynq_handle_autosuspend},
rita6966c302024-01-09 10:50:18 +0800168 {"NETLED",lynq_handle_netled},
169 {"STATUSLED",lynq_handle_statusled},
lichengzhang136c1842024-04-17 16:59:03 +0800170 {"DDRID",lynq_handle_ddr_identify},
lichengzhang51d33132024-10-17 14:36:42 +0800171 {"HALTENABLE",lynq_handle_halt_enable},
lichengzhang45091302023-10-08 02:10:34 -0700172 {NULL, NULL}
173};
174
175Command* find_command(char *input)
176{
177 ALOGD("function %s line %d input %s\n", __FUNCTION__, __LINE__, input);
178 int i;
179 int ret = -1;
180 for (i = 0; commands[i].cmd; i++)
181 {
182 ret = strncmp(input, commands[i].cmd, strlen(commands[i].cmd));
183 if(ret == 0)
184 {
185 ALOGD("function %s line %d find input %s commands[i].cmd %s strlen %d ret %d\n", __FUNCTION__, __LINE__, input, commands[i].cmd, strlen(commands[i].cmd), ret);
186 return (&commands[i]);
187 }
188 }
189 ALOGD("function %s line %d not find ret %d \n", __FUNCTION__, __LINE__, ret);
190 return ((Command *)NULL);
191}
192
193void lynq_at_common_cb(char *input, int input_max_size)
194{
195 if(handle_output != NULL)
196 {
197 ALOGD("function %s line %d input %s\n", __FUNCTION__, __LINE__, input);
198 if(input != NULL)
199 {
200 char *handle_string = input + strlen("AT+");
201 ALOGE("HJAHS:%s\n",handle_string);
202 if(!strlen(handle_string))
203 {
204 ALOGE("function %s line %d strlen %d\n", __FUNCTION__, __LINE__, strlen(handle_string));
205 return;
206 }
207 ALOGD("function %s line %d handle_string %s\n", __FUNCTION__, __LINE__, handle_string);
208 Command *cmd = find_command(handle_string);
209 if(cmd != NULL)
210 {
211 ALOGD("function %s line %d\n", __FUNCTION__, __LINE__);
212 (*(cmd->func))(handle_string);
213 return;
214 }
215 }
216 }
217}
218
219lynq_atsvc_incb lynq_register_at_common(lynq_atsvc_outcb out_cb)
220{
221 if(out_cb != NULL)
222 {
223 handle_output = out_cb;
224 ALOGD("function %s line %d\n", __FUNCTION__, __LINE__);
225 return lynq_at_common_cb;
226 }
jb.qi539afe42023-11-23 01:52:18 -0800227}