- MMS client: replaced asn1c code in mms_client_initiate.c

pull/21/head
Michael Zillgith 8 years ago
parent 311c70ea30
commit 325867b5f4

@ -133,7 +133,7 @@ mmsClient_getInvokeId(ConfirmedResponsePdu_t* confirmedResponse);
int int
mmsClient_write_out(void *buffer, size_t size, void *app_key); mmsClient_write_out(void *buffer, size_t size, void *app_key);
int void
mmsClient_createInitiateRequest(MmsConnection self, ByteBuffer* writeBuffer); mmsClient_createInitiateRequest(MmsConnection self, ByteBuffer* writeBuffer);
MmsPdu_t* MmsPdu_t*

@ -1,7 +1,7 @@
/* /*
* mms_client_initiate.c * mms_client_initiate.c
* *
* Copyright 2013, 2014 Michael Zillgith * Copyright 2013-2017 Michael Zillgith
* *
* This file is part of libIEC61850. * This file is part of libIEC61850.
* *
@ -22,7 +22,6 @@
*/ */
#include "libiec61850_platform_includes.h" #include "libiec61850_platform_includes.h"
#include <MmsPdu.h>
#include "mms_common.h" #include "mms_common.h"
#include "mms_common_internal.h" #include "mms_common_internal.h"
#include "mms_indication.h" #include "mms_indication.h"
@ -33,63 +32,80 @@
#include "mms_client_internal.h" #include "mms_client_internal.h"
#include "ber_encoder.h"
/* servicesSupported = {GetNameList} - required by initiate request, other services are required by some servers to work properly */ /* servicesSupported = {GetNameList} - required by initiate request, other services are required by some servers to work properly */
static uint8_t servicesSupported[] = { 0xee, 0x1c, 0x00, 0x00, 0x04, 0x08, 0x00, 0x00, 0x79, 0xef, 0x18 }; static uint8_t servicesSupported[] = { 0xee, 0x1c, 0x00, 0x00, 0x04, 0x08, 0x00, 0x00, 0x79, 0xef, 0x18 };
static InitiateRequestPdu_t void
createInitiateRequestPdu(MmsConnection self) mmsClient_createInitiateRequest(MmsConnection self, ByteBuffer* message)
{ {
InitiateRequestPdu_t request; int maxServerOutstandingCalling = DEFAULT_MAX_SERV_OUTSTANDING_CALLING;
int maxServerOutstandingCalled = DEFAULT_MAX_SERV_OUTSTANDING_CALLED;
int dataStructureNestingLevel = DEFAULT_DATA_STRUCTURE_NESTING_LEVEL;
request.localDetailCalling = (Integer32_t*) GLOBAL_CALLOC(1, sizeof(Integer32_t)); uint32_t localDetailSize =
*(request.localDetailCalling) = self->parameters.maxPduSize; BerEncoder_UInt32determineEncodedSize(self->parameters.maxPduSize);
request.proposedMaxServOutstandingCalled = DEFAULT_MAX_SERV_OUTSTANDING_CALLED; uint32_t proposedMaxServerOutstandingCallingSize =
request.proposedMaxServOutstandingCalling = DEFAULT_MAX_SERV_OUTSTANDING_CALLING; BerEncoder_UInt32determineEncodedSize(maxServerOutstandingCalling);
request.proposedDataStructureNestingLevel = (Integer8_t*) GLOBAL_CALLOC(1, sizeof(Integer8_t)); uint32_t proposedMaxServerOutstandingCalledSize =
*(request.proposedDataStructureNestingLevel) = DEFAULT_DATA_STRUCTURE_NESTING_LEVEL; BerEncoder_UInt32determineEncodedSize(maxServerOutstandingCalled);
request.mmsInitRequestDetail.proposedParameterCBB.size = 2; uint32_t dataStructureNestingLevelSize =
request.mmsInitRequestDetail.proposedParameterCBB.bits_unused = 5; BerEncoder_UInt32determineEncodedSize(dataStructureNestingLevel);
request.mmsInitRequestDetail.proposedParameterCBB.buf = (uint8_t*) GLOBAL_CALLOC(2, sizeof(uint8_t));
request.mmsInitRequestDetail.proposedParameterCBB.buf[0] = 0xf1;
request.mmsInitRequestDetail.proposedParameterCBB.buf[1] = 0x00;
request.mmsInitRequestDetail.proposedVersionNumber = 1; uint32_t initRequestDetailSize = 3 + 5 + 14;
request.mmsInitRequestDetail.servicesSupportedCalling.size = 11; uint32_t initiateRequestPduSize = 2 + localDetailSize +
request.mmsInitRequestDetail.servicesSupportedCalling.bits_unused = 3; 2 + proposedMaxServerOutstandingCallingSize +
2 + proposedMaxServerOutstandingCalledSize +
2 + dataStructureNestingLevelSize +
2 + initRequestDetailSize;
request.mmsInitRequestDetail.servicesSupportedCalling.buf = servicesSupported; /* encode message (InitiateRequestPdu) */
return request; int bufPos = 0;
} uint8_t* buffer = message->buffer;
static void bufPos = BerEncoder_encodeTL(0xa8, initiateRequestPduSize, buffer, bufPos);
freeInitiateRequestPdu(InitiateRequestPdu_t request)
{
GLOBAL_FREEMEM(request.localDetailCalling);
GLOBAL_FREEMEM(request.proposedDataStructureNestingLevel);
GLOBAL_FREEMEM(request.mmsInitRequestDetail.proposedParameterCBB.buf);
}
int /* localDetail */
mmsClient_createInitiateRequest(MmsConnection self, ByteBuffer* message) bufPos = BerEncoder_encodeTL(0x80, localDetailSize, buffer, bufPos);
{ bufPos = BerEncoder_encodeUInt32(self->parameters.maxPduSize, buffer, bufPos);
MmsPdu_t* mmsPdu = (MmsPdu_t*) GLOBAL_CALLOC(1, sizeof(MmsPdu_t));
/* proposedMaxServerOutstandingCalling */
bufPos = BerEncoder_encodeTL(0x81, proposedMaxServerOutstandingCallingSize, buffer, bufPos);
bufPos = BerEncoder_encodeUInt32(maxServerOutstandingCalling, buffer, bufPos);
/* proposedMaxServerOutstandingCalled */
bufPos = BerEncoder_encodeTL(0x82, proposedMaxServerOutstandingCalledSize, buffer, bufPos);
bufPos = BerEncoder_encodeUInt32(maxServerOutstandingCalled, buffer, bufPos);
/* proposedDataStructureNestingLevel */
bufPos = BerEncoder_encodeTL(0x83, dataStructureNestingLevelSize, buffer, bufPos);
bufPos = BerEncoder_encodeUInt32(dataStructureNestingLevel, buffer, bufPos);
mmsPdu->present = MmsPdu_PR_initiateRequestPdu; /* initRequestDetail */
bufPos = BerEncoder_encodeTL(0xa4, initRequestDetailSize, buffer, bufPos);
mmsPdu->choice.initiateRequestPdu = createInitiateRequestPdu(self); /* proposedVersionNumber = 1 */
buffer[bufPos++] = 0x80;
buffer[bufPos++] = 0x01;
buffer[bufPos++] = 0x01;
asn_enc_rval_t rval = der_encode(&asn_DEF_MmsPdu, mmsPdu, /* proposedParameterCBC */
(asn_app_consume_bytes_f*) mmsClient_write_out, (void*) message); buffer[bufPos++] = 0x81;
buffer[bufPos++] = 0x03;
buffer[bufPos++] = 0x05; /* padding */
buffer[bufPos++] = 0xf1;
buffer[bufPos++] = 0x00;
freeInitiateRequestPdu(mmsPdu->choice.initiateRequestPdu); /* servicesSupportedCalling */
GLOBAL_FREEMEM(mmsPdu); bufPos = BerEncoder_encodeBitString(0x82, 85, servicesSupported, buffer, bufPos);
return rval.encoded; message->size = bufPos;
} }
int int
@ -105,43 +121,112 @@ mmsClient_createConcludeRequest(MmsConnection self, ByteBuffer* message)
return -1; return -1;
} }
static bool
parseInitResponseDetail(MmsConnection self, uint8_t* buffer, int bufPos, int maxBufPos)
{
while (bufPos < maxBufPos) {
uint8_t tag = buffer[bufPos++];
int length;
bufPos = BerDecoder_decodeLength(buffer, &length, bufPos, maxBufPos);
if (bufPos < 0)
return false;
switch (tag) {
case 0x80: /* negotiated protocol version */
break;
case 0x81: /* parameter CBB */
break;
case 0x82: /* services supported called */
{
int i;
for (i = 0; i < 11; i++)
self->parameters.servicesSupported[i] = buffer[bufPos + i];
}
break;
default:
break;
}
bufPos += length;
}
return true;
}
bool bool
mmsClient_parseInitiateResponse(MmsConnection self) mmsClient_parseInitiateResponse(MmsConnection self)
{ {
bool result = false; bool result = false;
MmsPdu_t* mmsPdu = 0;
asn_dec_rval_t rval = ber_decode(NULL, &asn_DEF_MmsPdu, self->parameters.maxPduSize = CONFIG_MMS_MAXIMUM_PDU_SIZE;
(void**) &mmsPdu, ByteBuffer_getBuffer(self->lastResponse), self->parameters.dataStructureNestingLevel = DEFAULT_DATA_STRUCTURE_NESTING_LEVEL;
ByteBuffer_getSize(self->lastResponse)); self->parameters.maxServOutstandingCalled = DEFAULT_MAX_SERV_OUTSTANDING_CALLED;
self->parameters.maxServOutstandingCalling = DEFAULT_MAX_SERV_OUTSTANDING_CALLING;
if (rval.code != RC_OK) goto exit_function; int bufPos = 0;
int maxBufPos = ByteBuffer_getSize(self->lastResponse);
uint8_t* buffer = ByteBuffer_getBuffer(self->lastResponse);
if (mmsPdu->present == MmsPdu_PR_initiateResponsePdu) { while (bufPos < maxBufPos) {
InitiateResponsePdu_t* initiateResponse = &(mmsPdu->choice.initiateResponsePdu); uint8_t tag = buffer[bufPos++];
int length;
if (initiateResponse->localDetailCalled != NULL) bufPos = BerDecoder_decodeLength(buffer, &length, bufPos, maxBufPos);
self->parameters.maxPduSize = *(initiateResponse->localDetailCalled);
if (initiateResponse->negotiatedDataStructureNestingLevel != NULL) if (bufPos < 0) {
self->parameters.dataStructureNestingLevel = *(initiateResponse->negotiatedDataStructureNestingLevel);; // TODO write initiate error PDU!
return false;
}
self->parameters.maxServOutstandingCalled = initiateResponse->negotiatedMaxServOutstandingCalled; switch (tag) {
case 0x80: /* local-detail-calling */
self->parameters.maxPduSize = BerDecoder_decodeUint32(buffer, length, bufPos);
self->parameters.maxServOutstandingCalling = initiateResponse->negotiatedMaxServOutstandingCalling; if (self->parameters.maxPduSize > CONFIG_MMS_MAXIMUM_PDU_SIZE)
self->parameters.maxPduSize = CONFIG_MMS_MAXIMUM_PDU_SIZE;
int i; break;
for (i = 0; i < 11; i++) case 0x81: /* proposed-max-serv-outstanding-calling */
self->parameters.servicesSupported[i] = initiateResponse->mmsInitResponseDetail.servicesSupportedCalled.buf[i]; self->parameters.maxServOutstandingCalling = BerDecoder_decodeUint32(buffer, length, bufPos);
result = true; if (self->parameters.maxServOutstandingCalling > DEFAULT_MAX_SERV_OUTSTANDING_CALLING)
self->parameters.maxServOutstandingCalling = DEFAULT_MAX_SERV_OUTSTANDING_CALLING;
break;
case 0x82: /* proposed-max-serv-outstanding-called */
self->parameters.maxServOutstandingCalled = BerDecoder_decodeUint32(buffer, length, bufPos);
if (self->parameters.maxServOutstandingCalled > DEFAULT_MAX_SERV_OUTSTANDING_CALLED)
self->parameters.maxServOutstandingCalled = DEFAULT_MAX_SERV_OUTSTANDING_CALLED;
break;
case 0x83: /* proposed-data-structure-nesting-level */
self->parameters.dataStructureNestingLevel = BerDecoder_decodeUint32(buffer, length, bufPos);
break;
case 0xa4: /* mms-init-request-detail */
{
if (parseInitResponseDetail(self, buffer, bufPos, bufPos + length) == false)
return false;
}
break;
default:
break; /* Ignore unknown tags */
}
bufPos += length;
} }
else
result = false;
asn_DEF_MmsPdu.free_struct(&asn_DEF_MmsPdu, mmsPdu, 0);
exit_function:
return result; return result;
} }

Loading…
Cancel
Save