-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_api.c
129 lines (113 loc) · 2.62 KB
/
db_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <string.h>
#include "req_handler.h"
#ifdef UTFLAG
extern int call_by_test_svr_process_req4;
extern int call_by_test_svr_process_req5;
#endif
/* Find a spare terminal ID in range of 1-100 */
/* If no available terminal ID, set id = 0 */
/* Succeed, return 0; abormal error return 1 */
int
find_spare_id(int *id_ptr)
{
int id = 0;
for (int i=0;i < MAXTERMID; i++)
{
if (gl_terminal_info[i].flag == 0)
{
id = i + 1;
break;
}
}
*id_ptr = id;
#ifdef UTFLAG
if ( call_by_test_svr_process_req4 )
/* This is for unit testing */
return 1;
else
return 0;
#else
return 0;
#endif
}
/************************************************************************
* Insert an entry to global array gl_terminal_info[MAXTERMID] *
* return *
* Return values: *
* if inserted, return 0 *
* if no available terminal ID slot (equal to DB slot in this *
* design), *
* return 1 *
* abnormal error, return 2 (Not being used currently ) *
************************************************************************/
int
insert_db(struct terminal_info_struct *terminal_info_ptr)
{
int i;
for (i=0;i < MAXTERMID; i++)
{
if (gl_terminal_info[i].flag != 0 )
continue;
else
break;
}
if (i < MAXTERMID)
memcpy(&gl_terminal_info[i],terminal_info_ptr, sizeof(struct terminal_info_struct));
else if (i == MAXTERMID)
{
/* No available DB slot, which means no available terminal ID slot */
ERRLOG ("no available terminal ID slot.");
return 1;
}
else
{
/* should not reach here */
ERRLOG ("abnormal error.");
return 2;
}
#ifdef UTFLAG
if ( call_by_test_svr_process_req5 )
/* This is for unit testing */
return 2;
else
return 0;
#else
return 0;
#endif
}
/* Init all members with 0 or NULL for global array gl_terminal_info[MAXTERMID] */
void
init_db()
{
memset(&gl_terminal_info[0],0,sizeof(gl_terminal_info));
}
/* return 0, id found in DB */
/* return 1, id no found in DB , this could happen when client send a wrong id */
/* return 2, abnormal errors when query DB */
int
query_db(int id, struct terminal_info_struct *tm_db_ptr)
{
int found = 0;
if ( id <= 0 || id > MAXTERMID )
{
ERRLOG ("invalid terminal ID.");
return 2;
}
for (int i=0;i < MAXTERMID; i++)
{
if (gl_terminal_info[i].id == id )
{
memcpy(tm_db_ptr, &gl_terminal_info[i], sizeof(struct terminal_info_struct));
found = 1;
break;
}
}
if (found == 1)
return 0;
else
{
ERRLOG ("requested terminal ID not found in DB.");
return 1;
}
}