]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/handlers.cpp
COmpose CXXFLAGS for tests correctly.
[stg.git] / projects / stargazer / plugins / other / smux / handlers.cpp
1 #include <cassert>
2
3 #include "stg/GetRequest-PDU.h"
4 #include "stg/GetResponse-PDU.h"
5 #include "stg/VarBindList.h"
6 #include "stg/VarBind.h"
7
8 #include "stg/common.h"
9
10 #include "utils.h"
11 #include "smux.h"
12
13 #ifdef SMUX_DEBUG
14 bool SMUX::CloseHandler(const SMUX_PDUs_t * pdus)
15 {
16 printfd(__FILE__, "SMUX::CloseHandler()\n");
17 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
18 return true;
19 }
20 #else
21 bool SMUX::CloseHandler(const SMUX_PDUs_t *)
22 {
23 return true;
24 }
25 #endif
26
27 #ifdef SMUX_DEBUG
28 bool SMUX::RegisterResponseHandler(const SMUX_PDUs_t * pdus)
29 {
30 printfd(__FILE__, "SMUX::RegisterResponseHandler()\n");
31 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
32 return true;
33 }
34 #else
35 bool SMUX::RegisterResponseHandler(const SMUX_PDUs_t *)
36 {
37 return true;
38 }
39 #endif
40
41 bool SMUX::PDUsRequestHandler(const SMUX_PDUs_t * pdus)
42 {
43 #ifdef SMUX_DEBUG
44 printfd(__FILE__, "SMUX::PDUsRequestHandler()\n");
45 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
46 #endif
47 PDUsHandlers::iterator it(pdusHandlers.find(pdus->choice.pdus.present));
48 if (it != pdusHandlers.end())
49     {
50     return (this->*(it->second))(&pdus->choice.pdus);
51     }
52 #ifdef SMUX_DEBUG
53 else
54     {
55     switch (pdus->present)
56         {
57         case PDUs_PR_NOTHING:
58             printfd(__FILE__, "SMUX::PDUsRequestHandler() - nothing\n");
59             break;
60         case PDUs_PR_get_response:
61             printfd(__FILE__, "SMUX::PDUsRequestHandler() - get response\n");
62             break;
63         case PDUs_PR_trap:
64             printfd(__FILE__, "SMUX::PDUsRequestHandler() - trap\n");
65             break;
66         default:
67             printfd(__FILE__, "SMUX::PDUsRequestHandler() - undefined\n");
68         }
69     }
70 #endif
71 return true;
72 }
73
74 #ifdef SMUX_DEBUG
75 bool SMUX::CommitOrRollbackHandler(const SMUX_PDUs_t * pdus)
76 {
77 printfd(__FILE__, "SMUX::CommitOrRollbackHandler()\n");
78 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
79 return true;
80 }
81 #else
82 bool SMUX::CommitOrRollbackHandler(const SMUX_PDUs_t *)
83 {
84 return true;
85 }
86 #endif
87
88 bool SMUX::GetRequestHandler(const PDUs_t * pdus)
89 {
90 #ifdef SMUX_DEBUG
91 printfd(__FILE__, "SMUX::GetRequestHandler()\n");
92 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
93 #endif
94 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
95 GetResponse_PDU_t * msg = static_cast<GetResponse_PDU_t *>(calloc(1, sizeof(GetResponse_PDU_t)));
96 assert(msg && "Enought mempry to allocate GetResponse_PDU_t");
97 VarBindList_t * varBindList = &msg->variable_bindings;
98
99 long id = 0;
100 asn_INTEGER2long(&getRequest->request_id, &id);
101 asn_long2INTEGER(&msg->request_id, id);
102 asn_long2INTEGER(&msg->error_status, 0);
103 asn_long2INTEGER(&msg->error_index, 0);
104
105 const VarBindList_t * vbl = &getRequest->variable_bindings; 
106 for (int i = 0; i < vbl->list.count; ++i)
107     {
108     VarBind_t * vb = getRequest->variable_bindings.list.array[i];
109     Sensors::iterator it;
110     it = sensors.find(OID(&vb->name));
111     if (it == sensors.end())
112         {
113         return SendGetResponseErrorPDU(sock, getRequest,
114                                        PDU__error_status_noSuchName, i);
115         }
116
117     VarBind_t * newVb = static_cast<VarBind_t *>(calloc(1, sizeof(VarBind_t)));
118     assert(newVb && "Enought mempry to allocate VarBind_t");
119
120     it->first.ToOID(&newVb->name);
121     it->second->GetValue(&newVb->value);
122
123     ASN_SEQUENCE_ADD(varBindList, newVb);
124     }
125
126 bool res = SendGetResponsePDU(sock, msg);
127 #ifdef SMUX_DEBUG
128 asn_fprint(stderr, &asn_DEF_GetResponse_PDU, msg);
129 #endif
130 ASN_STRUCT_FREE(asn_DEF_GetResponse_PDU, msg);
131 return res;
132 }
133
134 bool SMUX::GetNextRequestHandler(const PDUs_t * pdus)
135 {
136 #ifdef SMUX_DEBUG
137 printfd(__FILE__, "SMUX::GetNextRequestHandler()\n");
138 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
139 #endif
140 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
141 GetResponse_PDU_t * msg = static_cast<GetResponse_PDU_t *>(calloc(1, sizeof(GetResponse_PDU_t)));
142 assert(msg && "Enought mempry to allocate GetResponse_PDU_t");
143 VarBindList_t * varBindList = &msg->variable_bindings;
144
145 long id = 0;
146 asn_INTEGER2long(&getRequest->request_id, &id);
147 asn_long2INTEGER(&msg->request_id, id);
148 asn_long2INTEGER(&msg->error_status, 0);
149 asn_long2INTEGER(&msg->error_index, 0);
150
151 const VarBindList_t * vbl = &getRequest->variable_bindings; 
152 for (int i = 0; i < vbl->list.count; ++i)
153     {
154     VarBind_t * vb = getRequest->variable_bindings.list.array[i];
155     Sensors::iterator it;
156     it = sensors.upper_bound(OID(&vb->name));
157     if (it == sensors.end())
158         {
159 #ifdef SMUX_DEBUG
160         printfd(__FILE__, "SMUX::GetNextRequestHandler() - '%s' not found\n", OID(&vb->name).ToString().c_str());
161 #endif
162         return SendGetResponseErrorPDU(sock, getRequest,
163                                        PDU__error_status_noSuchName, i);
164         }
165
166     VarBind_t * newVb = static_cast<VarBind_t *>(calloc(1, sizeof(VarBind_t)));
167     assert(newVb && "Enought mempry to allocate VarBind_t");
168
169     it->first.ToOID(&newVb->name);
170     it->second->GetValue(&newVb->value);
171
172     ASN_SEQUENCE_ADD(varBindList, newVb);
173     }
174
175 bool res = SendGetResponsePDU(sock, msg);
176 #ifdef SMUX_DEBUG
177 asn_fprint(stderr, &asn_DEF_PDU, msg);
178 #endif
179 ASN_STRUCT_FREE(asn_DEF_GetResponse_PDU, msg);
180 return res;
181 }
182
183 bool SMUX::SetRequestHandler(const PDUs_t * pdus)
184 {
185 #ifdef SMUX_DEBUG
186 printfd(__FILE__, "SMUX::SetRequestHandler()\n");
187 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
188 #endif
189 return SendGetResponseErrorPDU(sock, &pdus->choice.set_request,
190                                PDU__error_status_readOnly, 0);
191 }