Permalink
Browse files

Submit can split msg when need

  • Loading branch information...
1 parent 0d4b528 commit 75e79885175f9b5d69e3c00c1090db1d987838a5 @kaven276 committed Jun 4, 2012
Showing with 74 additions and 8 deletions.
  1. +74 −8 lib/cmds/cmdSubmit.js
View
82 lib/cmds/cmdSubmit.js
@@ -1,7 +1,9 @@
var bUnitText = (process.argv[1] === __filename)
, A = require('../AttrCfg.js')
- , override = require('../util.js').override
- , Encodings = require('../util.js').Encodings
+ , utl = require('../util.js')
+ , CmdSeq = require('../CmdSeq.js')
+ , override2 = utl.override2
+ , Encodings = utl.Encodings
, cannedOptions = {}
;
@@ -35,7 +37,7 @@ function CDefOptions(){
/**
* create a submit PDU object with required parameters and options
- * @param UserNumbers
+ * @param UserNumber
* @param MessageContent
* @param options may be a name-value object, or predefined option ID string, or array of ether of them
* @constructor
@@ -69,11 +71,75 @@ function Submit(UserNumber, MessageCoding, MessageContent, options){
}
this.MessageCoding = MessageCoding; // todo: for digit id only or automatic detected, default to what ??
- var enc = Encodings[MessageCoding.toString()];
- console.log('MessageContent', MessageContent)
- console.log(new Buffer(MessageContent, enc));
- this.MessageLength = (new Buffer(MessageContent, enc)).length;
- this.MessageContent = MessageContent;
+
+ if (MessageContent instanceof Buffer) {
+ this.MessageContent = MessageContent;
+ } else {
+ this.MessageContentOrigin = MessageContent;
+ switch (MessageCoding) {
+ case 0 : // acsii
+ this.MessageContent = new Buffer(MessageContent, 'ascii');
+ break;
+ case 8 : // ucs2
+ var bin = new Buffer(MessageContent, 'ucs2');
+ for (var i = 0, len = bin.length; i < len; i += 2) {
+ bin.writeUInt16BE(bin.readUInt16LE(i), i);
+ }
+ this.MessageContent = bin;
+ break;
+ case 4 : // binary
+ this.MessageContent = new Buffer(MessageContent, 'hex');
+ break;
+ default:
+ throw new Error('only encoding 0-ascii, 8-usc2, 4-binary(hex) is supported');
+ }
+ }
+
+ var msglen = this.MessageLength = this.MessageContent.length;
+
+ if (msglen > 254) {
+ // save original attributes
+ this.MessageContentLong = this.MessageContent;
+ this.MessageLengthLong = msglen;
+
+ var splits = this.splits = Math.ceil(msglen / (140 - 6));
+ var parts = this.MessageContentParts = new Buffer(msglen + 6 * splits);
+ var batchID = Math.random() * 256; // todo: how to determine the batchID
+ for (var i = 0; i < splits; i++) {
+ (new Buffer([0x05, 0x00, 0x03, batchID, splits, i + 1])).copy(parts, i * 140);
+ this.MessageContentLong.slice(i * (140 - 6), Math.min((i + 1) * (140 - 6), msglen)).copy(parts, i * 140 + 6);
+ }
+
+ // set to first part
+ this.MessageLength = 140;
+ this.MessageContent = parts.slice(0, 140);
+ this.options['TP_udhi'] = 1;
+ }
+}
+
+Submit.prototype.followParts = function(PDU){
+ var splits = this.splits
+ , len = PDU.length
+ , remain = this.MessageContentParts.length % 140
+ , PDUs = new Buffer(len * splits - (140 - remain))
+ , pos = len - 8 - 140
+ ;
+ PDU.copy(PDUs);
+ for (var i = 2; i < splits; i++) {
+ CmdSeq.genNextSeqSplit(PDU);
+ this.MessageContentParts.copy(PDU, pos, (i - 1) * 140, i * 140);
+ PDU.copy(PDUs, len * (i - 1));
+ }
+
+ PDU = PDU.slice(0, len - (140 - remain));
+ PDU.writeUInt32BE(PDU.length, 0);
+ CmdSeq.genNextSeqSplit(PDU);
+ PDU.writeUInt32BE(remain, pos - 4);
+ this.MessageContentParts.copy(PDU, pos, (splits - 1) * 140); // , (splits - 1) * 140 + remain
+ PDU.fill(0, PDU.length - 8);
+ lastlen = PDU.length;
+ PDU.copy(PDUs, len * (splits - 1));
+ return PDUs;
}
Submit.addCunnedOptions = function(name, options){

0 comments on commit 75e7988

Please sign in to comment.