summaryrefslogtreecommitdiff
path: root/remotecfg/device.c (plain)
blob: 000cb6ee532b67a1c103789c87d59d4855fbdd9f
1#include <stdio.h>
2#include <unistd.h>
3#include <errno.h>
4#include <sys/ioctl.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <errno.h>
9#include "common.h"
10
11int CheckVersion(int fd)
12{
13 char verBuf[64];
14
15 if (ioctl(fd, REMOTE_IOC_GET_DATA_VERSION, verBuf) < 0) {
16 fprintf(stderr, "get driver version error=%s\n",
17 strerror(errno));
18 return FAIL;
19 }
20 /*check share data version info*/
21 if (0 != strcasecmp(verBuf, SHARE_DATA_VERSION)) {
22 fprintf(stderr, "interface version not match:"\
23 "user[%s] unequal to kernel[%s]\n",
24 SHARE_DATA_VERSION, verBuf);
25 return FAIL;
26 }
27 return SUCC;
28}
29
30static int WriteSysFile(const char *dir, const char *fileName, unsigned int val)
31{
32 int fd;
33 int len;
34 char fileBuf[MAX_LINE_LEN];
35 char wBuf[32];
36
37 snprintf(fileBuf, MAX_LINE_LEN, "%s%s", dir, fileName);
38 if ((fd = open(fileBuf, O_RDWR)) < 0) {
39 fprintf(stderr, "open %s: %s\n", fileBuf, strerror(errno));
40 return FAIL;
41 }
42
43 len = snprintf(wBuf, 32, "%u", val);
44 if (write(fd, (void *)wBuf, len+1) < 0) {
45 fprintf(stderr, "write %s: %s\n", fileBuf, strerror(errno));
46 close(fd);
47 return FAIL;
48 }
49
50 close(fd);
51 return SUCC;
52}
53
54int SetCfgPara(int devFd, const char *sysDir, S_CFG_FILE_T *cfgFile)
55{
56 WriteSysFile(sysDir, "protocol", cfgFile->workMode);
57 WriteSysFile(sysDir, "repeat_enable", cfgFile->repeatEnable);
58 WriteSysFile(sysDir, "debug_enable", cfgFile->debugEnable);
59
60 if (ioctl(devFd, REMOTE_IOC_SET_SW_DECODE_PARA,
61 &cfgFile->sw_data) < 0) {
62 fprintf(stderr, "failed to set software decode paras: %s\n",
63 strerror(errno));
64 return FAIL;
65 }
66
67 return SUCC;
68}
69
70int SetTabPara(int devFd, S_TAB_FILE_T *tabFile)
71{
72 if (ioctl(devFd, REMOTE_IOC_SET_KEY_NUMBER,
73 &tabFile->tab.map_size) < 0 ) {
74 fprintf(stderr, "failed to set key number: %s\n",
75 strerror(errno));
76 return FAIL;
77 }
78
79 if (ioctl(devFd, REMOTE_IOC_SET_KEY_MAPPING_TAB,
80 &tabFile->tab) < 0 ) {
81 fprintf(stderr, "failed to set map table: %s\n",
82 strerror(errno));
83 return FAIL;
84 }
85
86 return SUCC;
87}
88
89int OpenDevice(char *filename)
90{
91 int fd;
92
93 if ((fd = open(filename, O_RDWR)) < 0)
94 fprintf(stderr, "open %s:%s\n", filename, strerror(errno));
95 return fd;
96}
97
98int CloseDevice(int fd)
99{
100 if (fd >= 0)
101 close(fd);
102 return 0;
103}
104