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