• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2022 Unionman Technology Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 
16 #include <string.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <signal.h>
21 #include "hilog/log.h"
22 #include "securec.h"
23 #include "um_gpio.h"
24 
UM_GPIO_Export(int gpioNum,int bExport)25 int UM_GPIO_Export(int gpioNum, int bExport)
26 {
27     int ret = -1;
28     char buffer[256] = {0};
29 
30     if (bExport) {
31         (void) snprintf_s(buffer, sizeof(buffer), sizeof(buffer), "echo %d > %s", gpioNum, UM_GPIO_EXPORT);
32     } else {
33         (void) snprintf_s(buffer, sizeof(buffer), sizeof(buffer), "echo %d > %s", gpioNum, UM_GPIO_UNEXPORT);
34     }
35 
36     sighandler_t old_handler;
37     old_handler = signal(SIGCHLD, SIG_DFL);
38     ret = system(buffer);
39     if (ret < 0) {
40         HILOG_ERROR(LOG_CORE, "set gpio%{public}d %{public}s failed", gpioNum, bExport == 1 ? "export" : "unexport");
41         return UM_GPIO_ERR;
42     }
43     (void) signal(SIGCHLD, old_handler);
44     return ret;
45 }
46 
UM_GPIO_SetDirection(int gpioNum,int direction)47 int UM_GPIO_SetDirection(int gpioNum, int direction)
48 {
49     int ret_sprintf_s = -1;
50 
51     // check gpio export or not
52     char gpio_file_name[128];
53     (void) memset_s(gpio_file_name, sizeof(gpio_file_name), 0, sizeof(gpio_file_name));
54     ret_sprintf_s = snprintf_s(gpio_file_name, sizeof(gpio_file_name), sizeof(gpio_file_name), "%s%d/direction",
55                                UM_GPIO_PEX, gpioNum);
56     if (ret_sprintf_s != 0) {
57     }
58 
59     if (access(gpio_file_name, F_OK) != 0) {
60         HILOG_ERROR(LOG_CORE, "gpio%{public}d not export", gpioNum);
61         return UM_GPIO_NOT_EXPROT_ERROR;
62     }
63     // set gpio direction
64 
65     FILE *fp = NULL;
66     fp = fopen(gpio_file_name, "r+");
67     if (fp == NULL) {
68         HILOG_ERROR(LOG_CORE, "open %{public}s%{public}d/direction failed", UM_GPIO_PEX, gpioNum);
69         return UM_GPIO_ERR;
70     }
71 
72     if (direction == UM_GPIO_DIRECTION_IN) {
73         fprintf(fp, "%s", "in");
74     } else if (direction == UM_GPIO_DIRECTION_OUT) {
75         fprintf(fp, "%s", "out");
76     }
77 
78     (void) fclose(fp);
79     fp = NULL;
80 
81     return 0;
82 }
83 
UM_GPIO_SetValue(int gpioNum,int value)84 int UM_GPIO_SetValue(int gpioNum, int value)
85 {
86     int ret_sprintf_s = -1;
87 
88     // check gpio export or not
89     char gpio_file_name[128];
90     (void) memset_s(gpio_file_name, sizeof(gpio_file_name), 0, sizeof(gpio_file_name));
91     ret_sprintf_s = snprintf_s(gpio_file_name, sizeof(gpio_file_name), sizeof(gpio_file_name), "%s%d/value",
92                                UM_GPIO_PEX, gpioNum);
93     if (ret_sprintf_s != 0) {
94     }
95 
96     if (access(gpio_file_name, F_OK) != 0) {
97         HILOG_ERROR(LOG_CORE, "gpio%{public}d not export", gpioNum);
98         return UM_GPIO_NOT_EXPROT_ERROR;
99     }
100     // set gpio value
101 
102     FILE *fp = NULL;
103     fp = fopen(gpio_file_name, "r+");
104     if (fp == NULL) {
105         HILOG_ERROR(LOG_CORE, "open %{public}s%{public}d/value failed", UM_GPIO_PEX, gpioNum);
106         return UM_GPIO_ERR;
107     }
108 
109     if (value == UM_GPIO_LOW_LEVE) {
110         fprintf(fp, "%s", "0");
111     } else if (value == UM_GPIO_HIGH_LEVE) {
112         fprintf(fp, "%s", "1");
113     }
114 
115     (void) fclose(fp);
116     fp = NULL;
117 
118     return 0;
119 }
120 
UM_GPIO_IsExport(int gpioNum,int * value)121 int UM_GPIO_IsExport(int gpioNum, int *value)
122 {
123     int ret_sprintf_s = -1;
124 
125     if (value == NULL) {
126         return UM_GPIO_ERR;
127     }
128     // check gpio export or not
129     char gpio_file_name[128];
130     (void) memset_s(gpio_file_name, sizeof(gpio_file_name), 0, sizeof(gpio_file_name));
131     ret_sprintf_s = snprintf_s(gpio_file_name, sizeof(gpio_file_name), sizeof(gpio_file_name), "%s%d/value",
132                                UM_GPIO_PEX, gpioNum);
133     if (ret_sprintf_s != 0) {
134     }
135 
136     if (access(gpio_file_name, F_OK) != 0) {
137         HILOG_INFO(LOG_CORE, "gpio%{public}d not export", gpioNum);
138         *value = UM_GPIO_NOT_EXPORT;
139     } else {
140         *value = UM_GPIO_EXPORTED;
141     }
142     return 0;
143 }
144 
UM_GPIO_GetDirection(int gpioNum,int * value)145 int UM_GPIO_GetDirection(int gpioNum, int *value)
146 {
147     int ret = 0;
148     int ret_sprintf_s = -1;
149 
150     if (value == NULL) {
151         return UM_GPIO_ERR;
152     }
153     // check gpio export or not
154     char gpio_file_name[128];
155     (void) memset_s(gpio_file_name, sizeof(gpio_file_name), 0, sizeof(gpio_file_name));
156     ret_sprintf_s = snprintf_s(gpio_file_name, sizeof(gpio_file_name), sizeof(gpio_file_name), "%s%d/direction",
157                                UM_GPIO_PEX, gpioNum);
158     if (ret_sprintf_s != 0) {
159     }
160 
161     if (access(gpio_file_name, F_OK) != 0) {
162         HILOG_ERROR(LOG_CORE, "gpio%{public}d not export", gpioNum);
163         return UM_GPIO_NOT_EXPROT_ERROR;
164     }
165     // get gpio direction
166 
167     FILE *fp = NULL;
168     char buffer[20] = {0};
169     fp = fopen(gpio_file_name, "r");
170     if (fp == NULL) {
171         HILOG_ERROR(LOG_CORE, "read %{public}s%{public}d/direction failed", UM_GPIO_PEX, gpioNum);
172         return UM_GPIO_ERR;
173     }
174     (void) fread(buffer, sizeof(buffer), 1, fp);
175     (void) fclose(fp);
176     fp = NULL;
177     if (strstr(buffer, "out") != NULL) {
178         *value = UM_GPIO_DIRECTION_OUT;
179     } else if (strstr(buffer, "in") != NULL) {
180         *value = UM_GPIO_DIRECTION_IN;
181     } else {
182         ret = UM_GPIO_ERR;
183     }
184     return ret;
185 }
186 
UM_GPIO_GetValue(int gpioNum,int * value)187 int UM_GPIO_GetValue(int gpioNum, int *value)
188 {
189     int ret = 0;
190     int ret_sprintf_s = -1;
191 
192     if (value == NULL) {
193         return UM_GPIO_ERR;
194     }
195     // check gpio export or not
196     char gpio_file_name[128];
197     (void) memset_s(gpio_file_name, sizeof(gpio_file_name), 0, sizeof(gpio_file_name));
198     ret_sprintf_s = snprintf_s(gpio_file_name, sizeof(gpio_file_name), sizeof(gpio_file_name), "%s%d/value",
199                                UM_GPIO_PEX, gpioNum);
200     if (ret_sprintf_s != 0) {
201     }
202 
203     if (access(gpio_file_name, F_OK) != 0) {
204         HILOG_ERROR(LOG_CORE, "gpio%{public}d not export", gpioNum);
205         return UM_GPIO_NOT_EXPROT_ERROR;
206     }
207     // get gpio value
208 
209     FILE *fp = NULL;
210     char buffer[20] = {0};
211     fp = fopen(gpio_file_name, "r");
212     if (fp == NULL) {
213         HILOG_ERROR(LOG_CORE, "read %{public}s%{public}d/value failed", UM_GPIO_PEX, gpioNum);
214         return UM_GPIO_ERR;
215     }
216     (void) fread(buffer, sizeof(buffer), 1, fp);
217     (void) fclose(fp);
218     fp = NULL;
219     if (strstr(buffer, "0") != NULL) {
220         *value = UM_GPIO_LOW_LEVE;
221     } else if (strstr(buffer, "1") != NULL) {
222         *value = UM_GPIO_HIGH_LEVE;
223     } else {
224         ret = UM_GPIO_ERR;
225     }
226     return ret;
227 }