1 #include "asn1/OpenPDU.h"
2 #include "asn1/ClosePDU.h"
3 #include "asn1/RReqPDU.h"
4 #include "asn1/GetRequest-PDU.h"
5 #include "asn1/GetResponse-PDU.h"
6 #include "asn1/VarBindList.h"
7 #include "asn1/VarBind.h"
8 #include "asn1/OBJECT_IDENTIFIER.h"
9 #include "asn1/ber_decoder.h"
10 #include "asn1/der_encoder.h"
12 #include "stg/common.h"
16 std::string OI2String(OBJECT_IDENTIFIER_t * oi);
17 int SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse);
18 int SendGetResponseErrorPDU(int fd,
19 const PDU_t * getRequest,
23 bool SMUX::CloseHandler(const SMUX_PDUs_t * pdus)
25 printfd(__FILE__, "SMUX::CloseHandler()\n");
26 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
30 bool SMUX::RegisterResponseHandler(const SMUX_PDUs_t * pdus)
32 printfd(__FILE__, "SMUX::RegisterResponseHandler()\n");
33 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
37 bool SMUX::PDUsRequestHandler(const SMUX_PDUs_t * pdus)
39 printfd(__FILE__, "SMUX::PDUsRequestHandler()\n");
40 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
41 PDUsHandlers::iterator it;
42 it = pdusHandlers.find(pdus->choice.pdus.present);
43 if (it != pdusHandlers.end())
45 return (this->*(it->second))(&pdus->choice.pdus);
49 switch (pdus->present)
52 printfd(__FILE__, "SMUX::PDUsRequestHandler() - nothing\n");
54 case PDUs_PR_get_response:
55 printfd(__FILE__, "SMUX::PDUsRequestHandler() - get response\n");
58 printfd(__FILE__, "SMUX::PDUsRequestHandler() - trap\n");
61 printfd(__FILE__, "SMUX::PDUsRequestHandler() - undefined\n");
67 bool SMUX::CommitOrRollbackHandler(const SMUX_PDUs_t * pdus)
69 printfd(__FILE__, "SMUX::CommitOrRollbackHandler()\n");
70 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
74 bool SMUX::GetRequestHandler(const PDUs_t * pdus)
76 printfd(__FILE__, "SMUX::GetRequestHandler()\n");
77 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
78 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
79 GetResponse_PDU_t msg;
80 VarBindList_t * varBindList = &msg.variable_bindings;
81 memset(&msg, 0, sizeof(msg));
83 msg.request_id = getRequest->request_id;
84 asn_long2INTEGER(&msg.error_status, 0);
85 asn_long2INTEGER(&msg.error_index, 0);
87 const VarBindList_t * vbl = &getRequest->variable_bindings;
88 for (int i = 0; i < vbl->list.count; ++i)
90 VarBind_t * vb = getRequest->variable_bindings.list.array[i];
92 it = sensors.find(OI2String(&vb->name));
93 if (it == sensors.end())
95 SendGetResponseErrorPDU(sock, getRequest,
96 PDU__error_status_noSuchName, i);
101 memset(&newVb, 0, sizeof(newVb));
103 newVb.name = vb->name;
104 it->second->GetValue(&newVb.value);
106 ASN_SEQUENCE_ADD(varBindList, &newVb);
109 SendGetResponsePDU(sock, &msg);
110 asn_fprint(stderr, &asn_DEF_PDU, &msg);
114 bool SMUX::GetNextRequestHandler(const PDUs_t * pdus)
116 printfd(__FILE__, "SMUX::GetNextRequestHandler()\n");
117 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
118 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
119 GetResponse_PDU_t msg;
120 VarBindList_t * varBindList = &msg.variable_bindings;
121 memset(&msg, 0, sizeof(msg));
123 msg.request_id = getRequest->request_id;
124 asn_long2INTEGER(&msg.error_status, 0);
125 asn_long2INTEGER(&msg.error_index, 0);
127 const VarBindList_t * vbl = &getRequest->variable_bindings;
128 for (int i = 0; i < vbl->list.count; ++i)
130 VarBind_t * vb = getRequest->variable_bindings.list.array[i];
131 Sensors::iterator it;
132 it = sensors.upper_bound(OI2String(&vb->name));
133 if (it == sensors.end())
135 SendGetResponseErrorPDU(sock, getRequest,
136 PDU__error_status_noSuchName, i);
141 memset(&newVb, 0, sizeof(newVb));
143 newVb.name = vb->name;
144 it->second->GetValue(&newVb.value);
146 ASN_SEQUENCE_ADD(varBindList, &newVb);
149 SendGetResponsePDU(sock, &msg);
150 asn_fprint(stderr, &asn_DEF_PDU, &msg);
154 bool SMUX::SetRequestHandler(const PDUs_t * pdus)
156 printfd(__FILE__, "SMUX::SetRequestHandler()\n");
157 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
158 SendGetResponseErrorPDU(sock, &pdus->choice.set_request,
159 PDU__error_status_readOnly, 0);
163 std::string OI2String(OBJECT_IDENTIFIER_t * oi)
168 int count = OBJECT_IDENTIFIER_get_arcs(oi, arcs, sizeof(arcs[0]), 1024);
173 for (int i = 0; i < count; ++i)
177 strprintf(&arc, "%d", arcs[i]);
184 int SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse)
186 asn_enc_rval_t error;
189 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, getResponse, buffer,
192 if (error.encoded == -1)
194 printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n",
195 error.failed_type ? error.failed_type->name : "unknown");
200 write(fd, buffer, error.encoded);
201 printfd(__FILE__, "GetResponsePDU encoded successfully to %d bytes\n",
207 int SendGetResponseErrorPDU(int fd,
208 const PDU_t * getRequest,
212 asn_enc_rval_t error;
213 GetResponse_PDU_t msg;
215 memset(&msg, 0, sizeof(msg));
217 msg.request_id = getRequest->request_id;
218 asn_long2INTEGER(&msg.error_status, errorStatus);
219 asn_long2INTEGER(&msg.error_index, errorIndex);
222 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer,
225 if (error.encoded == -1)
227 printfd(__FILE__, "Could not encode GetResponsePDU for error (at %s)\n",
228 error.failed_type ? error.failed_type->name : "unknown");
233 write(fd, buffer, error.encoded);
235 "GetResponsePDU for error encoded successfully to %d bytes\n",