Esempio n. 1
0
func (sm *StateMachine) sendSMS(sms *C.GSM_SMSMessage, number string, report bool) error {
	C.CopyUnicodeString(&sms.SMSC.Number[0], &sm.smsc.Number[0])
	decodeUTF8(&sms.Number[0], number)
	if report {
		sms.PDU = C.SMS_Status_Report
	} else {
		sms.PDU = C.SMS_Submit
	}
	// Send mepssage
	sm.status = C.ERR_TIMEOUT
	if e := C.GSM_SendSMS(sm.g, sms); e != C.ERR_NONE {
		return Error{"SendSMS", e}
	}
	// Wait for reply
	t := time.Now()
	for time.Now().Sub(t) < sm.Timeout {
		C.GSM_ReadDevice(sm.g, C.TRUE)
		if sm.status == C.ERR_NONE {
			// Message sent OK
			break
		} else if sm.status != C.ERR_TIMEOUT {
			// Error
			break
		}
	}
	if sm.status != C.ERR_NONE {
		return Error{"ReadDevice", sm.status}
	}
	return nil
}
Esempio n. 2
0
// Sends message
func (g *GSM) SendSMS(text, number string) (err error) {
	var sms C.GSM_SMSMessage
	var smsc C.GSM_SMSC

	sms.PDU = C.SMS_Submit                           // submit message
	sms.UDH.Type = C.UDH_NoUDH                       // no UDH, just a plain message
	sms.Coding = C.SMS_Coding_Default_No_Compression // default coding for text
	sms.Class = 1                                    // class 1 message (normal)

	C.EncodeUnicode((*C.uchar)(unsafe.Pointer(&sms.Text)), C.CString(text), C.int(len(text)))
	C.EncodeUnicode((*C.uchar)(unsafe.Pointer(&sms.Number)), C.CString(number), C.int(len(number)))

	// we need to know SMSC number
	smsc.Location = 1
	e := C.GSM_GetSMSC(g.sm, &smsc)
	if e != ERR_NONE {
		err = errors.New(errorString(int(e)))
		return
	}

	// set SMSC number in message
	sms.SMSC.Number = smsc.Number

	// Set flag before callind SendSMS, some phones might give instant response
	smsSendStatus = ERR_TIMEOUT

	// send message
	e = C.GSM_SendSMS(g.sm, &sms)
	if e != ERR_NONE {
		err = errors.New(errorString(int(e)))
		return
	}

	// wait for network reply
	for {
		C.GSM_ReadDevice(g.sm, C.gboolean(1))
		if smsSendStatus == ERR_NONE {
			break
		}
		if smsSendStatus != ERR_TIMEOUT {
			err = errors.New(errorString(int(smsSendStatus)))
			break
		}
	}

	return
}