-
Notifications
You must be signed in to change notification settings - Fork 3
/
REDIS_wrapper.cpp
164 lines (132 loc) · 5.15 KB
/
REDIS_wrapper.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <math.h>
#include "REDIS_custom.h"
/*
* Output functions
*
*/
void REDIS_Outputs_wrapper(void *SS,
redisContext *c,
const uint8_T *input,
uint8_T *data ,
int32_T *status,
uint8_T *replyType,
uint32_T *numElements,
uint32_T data_width,
int_T tid)
{
SimStruct *S = (SimStruct *) SS;
// Set REDIS_ERR by default
*status = -1;
// Set reply type as REDIS_REPLY_ERROR by default
*replyType = 6;
// Set num elements to zero by default.
*numElements = 0;
redisReply *reply = REDIS_send_command(c,(char *)input);
// Parse response.
REDIS_Outputs_parse_response(S, c, reply, data, status, replyType, numElements, data_width, tid);
}
void REDIS_Outputs_parse_response(SimStruct *S, redisContext *c, redisReply *reply, uint8_T *data, int32_T *status, uint8_T *replyType, uint32_T *numElements, uint32_T data_width, int_T tid){
if(reply == NULL || c->err){
return;
}
*replyType = reply->type;
*status = c->err;
// Empty data output port
memset(data, 0, data_width);
REDIS_started(S,tid);
int len = 0;
// PARSE RESPONSE
switch(reply->type){
case REDIS_REPLY_ERROR:
//printf("RESPONSE TYPE: REDIS_REPLY_ERROR\n");
*numElements = 0;
break;
case REDIS_REPLY_ARRAY:
//printf("RESPONSE TYPE: REDIS_REPLY_ARRAY\n");
*numElements = reply->elements;
for (int j = 0; j < reply->elements; j++) {
len = strlen(reply->element[j]->str);
//printf("%u) (data_width: %d): %s\n",j, len,reply->element[j]->str);
// Copy redis element to data output port.
if(len < data_width){
memcpy(data, reply->element[j]->str, len);
REDIS_received(S,tid);
memset(data, 0, len);
}
}
break;
case REDIS_REPLY_INTEGER:
//printf("RESPONSE TYPE: REDIS_REPLY_INTEGER\n");
//printf("0): %d\n",reply->integer);
// Convert integer to string (max 30 bytes)
char num_str[30];
sprintf(num_str, "%lu", reply->integer);
memcpy(data, &num_str, strlen(num_str));
*numElements = 1;
REDIS_received(S,tid);
memset(data, 0, strlen(num_str));
break;
case REDIS_REPLY_STRING:
//printf("RESPONSE TYPE: REDIS_REPLY_STRING\n");
//printf("0): %s\n",reply->str);
len = strlen(reply->str);
if(len < data_width){
memcpy(data, reply->str, len);
*numElements = 1;
REDIS_received(S,tid);
memset(data, 0, len);
}else{
printf("ERROR 0): %s . Not enough memory to copy element\n",reply->str);
}
break;
case REDIS_REPLY_STATUS:
//printf("RESPONSE TYPE: REDIS_REPLY_STATUS\n");
//printf("0): %s\n",reply->str);
len = strlen(reply->str);
if(len < data_width){
memcpy(data, reply->str, len);
*numElements = 1;
REDIS_received(S,tid);
memset(data, 0, len);
}else{
printf("ERROR 0): %s . Not enough memory to copy element\n",reply->str);
}
break;
case REDIS_REPLY_NIL:
//printf("RESPONSE TYPE: REDIS_REPLY_NIL\n");
//printf("0): NULL\n");
*numElements = 1;
REDIS_received(S,tid);
break;
}
// FINISHED
REDIS_finished(S,tid);
memset(data, 0, len);
}
bool REDIS_received(SimStruct *S, int_T tid){
// RECEIVED
if (!ssCallSystemWithTid(S,0,tid)) {
/* Error occurred, which will be reported by */
/*the Simulink engine*/
return false;
}
return true;
}
bool REDIS_finished(SimStruct *S, int_T tid){
// FINISHED
if (!ssCallSystemWithTid(S,1,tid)) {
/* Error occurred, which will be reported by */
/*the Simulink engine*/
return false;
}
return true;
}
bool REDIS_started(SimStruct *S, int_T tid){
// FINISHED
if (!ssCallSystemWithTid(S,2,tid)) {
/* Error occurred, which will be reported by */
/*the Simulink engine*/
return false;
}
return true;
}