# Problem with Return a message to sender - sms sample

**URL:** <https://forum.legato.io/t/problem-with-return-a-message-to-sender-sms-sample/6370>\
**Category:** Legato Application Framework\
**Created:** [December 9, 2022, 8:41am UTC](https://forum.legato.io/t/problem-with-return-a-message-to-sender-sms-sample/6370 "2022-12-09T08:41:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![keysling2207](https://avatars.discourse-cdn.com/v4/letter/k/3e96dc/32.png) [@keysling2207](https://forum.legato.io/u/keysling2207)\
**Post date:** [December 9, 2022, 8:41am UTC](https://forum.legato.io/t/problem-with-return-a-message-to-sender-sms-sample/6370/1 "2022-12-09T08:41:53Z")

</div>

Hello,

I want to implement a function from the smsMT.c source code ( [legato-af/smsMT.c at master · legatoproject/legato-af (github.com)](https://github.com/legatoproject/legato-af/blob/master/apps/sample/sms/smsClient/smsMT.c)) of sms sample: if module receive a message with the content “hello” from any phone number, the module will send message to my phone with the content:  
“hello on phone number: -phone number above-”.

I adapted the code like this, successfully built and installed on my WP7607. But  
the module just shows as “hello” on terminal and doesn’t send any messages to my phone.

My module connects to the network and works normally. Messages can be sent using AT+CMGS command

```auto
#include "legato.h"
#include "interfaces.h"
#include "smsSample.h"

//#define MESSAGE_FEEDBACK "hello on phone number %s"

static le_sms_RxMessageHandlerRef_t RxHdlrRef;
static le_sms_FullStorageEventHandlerRef_t FullStorageHdlrRef;

//--------------------------------------------------------------------------------------------------
/**
 * Handler function for SMS message reception.
 *
 */
//--------------------- -----------------------------------------------------------------------------
static void RxMessageHandler
(
    le_sms_MsgRef_t msgRef,
    void* contextPtr
)
{
    le_result_t res;
    char tel[LE_MDMDEFS_PHONE_NUM_MAX_BYTES];
    char timestamp[LE_SMS_TIMESTAMP_MAX_BYTES] = {0};
    char text[LE_SMS_TEXT_MAX_BYTES] = {0};
    char textReturn[LE_SMS_TEXT_MAX_BYTES] = {0};

    LE_INFO("A New SMS message is received with ref.%p", msgRef);

    if (le_sms_GetFormat(msgRef) == LE_SMS_FORMAT_TEXT)
    {
        res = le_sms_GetSenderTel(msgRef, tel, sizeof(tel));
        if(res != LE_OK)
        {
            LE_ERROR("le_sms_GetSenderTel has failed (res.%d)!", res);
        }
        else
        {
            LE_INFO("Message is received from %s.", tel);
        }

        res = le_sms_GetTimeStamp(msgRef, timestamp, sizeof(timestamp));
        if(res != LE_OK)
        {
            LE_ERROR("le_sms_GetTimeStamp has failed (res.%d)!", res);
        }
        else
        {
            LE_INFO("Message timestamp is %s.", timestamp);
        }

        res = le_sms_GetText(msgRef, text, sizeof(text));
        if(res != LE_OK)
        {
            LE_ERROR("le_sms_GetText has failed (res.%d)!", res);
        }
        else
        {
            LE_INFO("Message content: \"%s\"", text);
            
            
        if(strcmp(text,"hello")==0)
        {
	snprintf(textReturn, sizeof(textReturn), "hello on phone number %s", tel);
        // Return a message to sender with phone number include (see smsMO.c file)
        res = smsmo_SendMessage("0908334462", textReturn);
        if (res != LE_OK)
        {
            LE_ERROR("SmsMoMessage has failed (res.%d)!", res);
        }
        else
        {
            LE_INFO("the message has been successfully sent.");
        }
        }

        }

        res = le_sms_DeleteFromStorage(msgRef);
        if(res != LE_OK)
        {
            LE_ERROR("le_sms_DeleteFromStorage has failed (res.%d)!", res);
        }
        else
        {
            LE_INFO("the message has been successfully deleted from storage.");
        }
    }
    else
    {
        LE_WARN("Warning! I read only Text messages!");
    }

    le_sms_Delete(msgRef);
}

//--------------------------------------------------------------------------------------------------
/**
 * Handler function for SMS storage full message indication.
 *
 */
//--------------------- -----------------------------------------------------------------------------
static void StorageMessageHandler
(
    le_sms_Storage_t storage,
    void* contextPtr
)
{
    LE_INFO("A Full storage SMS message is received. Type of full storage %d", storage);
}

//--------------------------------------------------------------------------------------------------
/**
 * This function installs an handler for message reception.
 *
 * @return LE_FAULT The function failed.
 * @return LE_OK The function succeed.
 */
//--------------------------------------------------------------------------------------------------
le_result_t smsmt_Receiver
(
    void
)
{
    RxHdlrRef = le_sms_AddRxMessageHandler(RxMessageHandler, NULL);
    if (!RxHdlrRef)
    {
        LE_ERROR("le_sms_AddRxMessageHandler has failed!");
        return LE_FAULT;
    }
    else
    {
        return LE_OK;
    }
}

//--------------------------------------------------------------------------------------------------
/**
 * This function installs an handler for storage message indication.
 *
 * @return LE_FAULT The function failed.
 * @return LE_OK The function succeed.
 */
//--------------------------------------------------------------------------------------------------
le_result_t smsmt_MonitorStorage
(
    void
)
{
    FullStorageHdlrRef = le_sms_AddFullStorageEventHandler(StorageMessageHandler, NULL);
    if (!FullStorageHdlrRef)
    {
        LE_ERROR("le_sms_AddFullStorageEventHandler has failed!");
        return LE_FAULT;
    }
    else
    {
        return LE_OK;
    }
}

//--------------------------------------------------------------------------------------------------
/**
 * This function remove the handler for message reception.
 *
 */
//--------------------------------------------------------------------------------------------------
void smsmt_HandlerRemover
(
    void
)
{
    le_sms_RemoveRxMessageHandler(RxHdlrRef);
}

//--------------------------------------------------------------------------------------------------
/**
 * This function remove the handler for storage message indication.
 *
 */
//--------------------------------------------------------------------------------------------------
void smsmt_StorageHandlerRemover
(
    void
)
{
    le_sms_RemoveFullStorageEventHandler(FullStorageHdlrRef);
}

```

Am I missing something?

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [December 9, 2022, 8:47am UTC](https://forum.legato.io/t/problem-with-return-a-message-to-sender-sms-sample/6370/2 "2022-12-09T08:47:10Z")

</div>

have you used the cm tool to send sms to peer phone?

> **[cm - Legato Docs](https://docs.legato.io/latest/toolsTarget_cm.html#toolsTarget_cm_sms)**
>
> legato™ is an open source Linux-based embedded platform designed to simplify connected IoT application development

If it is working fine, you can use the code of cm tool:

> <https://github.com/legatoproject/legato-af/blob/master/apps/tools/cm/cm_sms.c>

---

<div class="post-metadata">

**Author:** ![keysling2207](https://avatars.discourse-cdn.com/v4/letter/k/3e96dc/32.png) [@keysling2207](https://forum.legato.io/u/keysling2207)\
**Post date:** [December 9, 2022, 9:31am UTC](https://forum.legato.io/t/problem-with-return-a-message-to-sender-sms-sample/6370/3 "2022-12-09T09:31:05Z")

</div>

I was able to use cm tool to send message to my phone. But what I want the module send a message with content: “hello on phone number: …” to my phone when module receive message with content: “hello” from any phone number.  
It’s strange that even in the source code smsMT.c there is a function “Return a message to sender with phone number include” but it doesn’t work.

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [December 9, 2022, 10:21am UTC](https://forum.legato.io/t/problem-with-return-a-message-to-sender-sms-sample/6370/4 "2022-12-09T10:21:33Z")

</div>

have you written a legato application to send SMS successfully?

---

<div class="post-metadata">

**Author:** ![keysling2207](https://avatars.discourse-cdn.com/v4/letter/k/3e96dc/32.png) [@keysling2207](https://forum.legato.io/u/keysling2207)\
**Post date:** [December 9, 2022, 10:58am UTC](https://forum.legato.io/t/problem-with-return-a-message-to-sender-sms-sample/6370/5 "2022-12-09T10:58:34Z")

</div>

I think I found something wrong. I check “app status” and see both sms and smsApp are running. And when I stop smsApp. I can send messages already. Also thanks for the quick reply.
