cancel
Showing results for 
Search instead for 
Did you mean: 

ModusToolbox General

New Contributor

Hi,

I've glanced some portions of C# source code of the CySmart API usage, however, this is not a complete example.

I need to establish a L2Cap channel connection and exchange data through it.

The L2CapMgr has to be initialized and the channel need to be established.

Then data can be sent using the L2CapMgr callback with method SendData.

This is waht, at least, I've understood.

Where can I find a full featured example code gathering all the steps?

Many thanks,

0 Likes
Reply
1 Solution
Moderator
Moderator

Hello Pascal,

Here's how you use the SendData function.

Create a global instance variable of ICyL2CapChannel as shown below:

// Peer BLE device

ICyBleDevice Peripheral;

public static ICyL2CapChannel cyL2CapChannel;

In the previous interaction, I explained how you need to call EstablishChannel API.

CyEstablishL2CapChannelInfo l2CapChannelInfo = new CyEstablishL2CapChannelInfo(0, 0, 512, 10, 1000);

CyL2CapMgrCallback l2capCallbackFunc = new CyL2CapMgrCallback();

Peripheral.L2CapMgr.RegisterL2CapCallback(l2capCallbackFunc);

Peripheral.L2CapMgr.EstablishChannel(l2CapChannelInfo);

When the EstablishChannel API is called, a callback method OnChannelEstablished is executed when the channel is established.

You can check the status of this operation and if successful, pass the established channel info to your variable like this.

class l2capCallbackFunc : CyL2CapMgrCallback

{

     public override void OnChannelEstablished(ICyL2CapChannel channel, CyStatus status)

     {

        if (status == CyStatus.BLE_STATUS_OK) {

           cyL2CapChannel = channel;

         }

      }

      public override void OnDataReceived(CyL2CapDataReceivedInfo info)

      {

          base.OnDataReceived(info);

      }

      public override void OnSendData(ushort channelID, CyStatus status)

            {

                base.OnSendData(channelID, status);

            }

        }

You can then use this in your SendData function in this fashion:

CyL2CapSendDataInfo l2CapSendDataInfo = new CyL2CapSendDataInfo(cyL2CapChannel, BitConverter.GetBytes(0x001));

Peripheral.L2CapMgr.SendData(l2CapSendDataInfo);

Once the data is sent, you can check the callback method OnSendData for the status of the transaction as well.

Hope this helps

Regards,
Dheeraj

View solution in original post

6 Replies
Moderator
Moderator

Hi,

As of now we have only the CySmart 1.3 application for windows.

Have you gone through the CySmart API documentation present at the following path in your PC? If not please go through it. It will have APIs for L2CAP functionality. Or please find the attached.

C:\Program Files (x86)\Cypress\CySmart\1.3\documentation

You can also go through the attached PSoC Creator code examples for central and peripheral which uses L2CAP APIs to send and receive data.

Thanks

Ganesh

0 Likes
Reply
New Contributor

Hi Ganesh,

Thanks for your response but actually you sent me the source code on the MCU side.

I rather need example that work on Windows.

I'm developing the Windows application to exchange with the Cypress module.

I've found some C# examples of the API usage but not sufficiently to make a complete connection and data exchange over L2CAP Channel.

Do you have such example of API usage?

0 Likes
Reply
Moderator
Moderator

We do not have any fully featured example that can demonstrate this. But I can help you understand how the APIs need to be used.

The L2CapMgr is a property of the ICyBleDevice interface.

You should already have an instance of this interface in your application. For example, lets say it is:

ICyBleDevice Peripheral;

Once all the initializations are complete and you want to now establish a L2Cap channel connection and exchange data through it. Here's what you need to do.

Let's say you have a function for doing this called l2capTranferInit(). Write the following lines of code:

CyEstablishL2CapChannelInfo l2CapChannelInfo = new CyEstablishL2CapChannelInfo(0, 0, 512, 10, 1000);

CyL2CapMgrCallback l2capCallbackFunc = new CyL2CapMgrCallback();

Peripheral.L2CapMgr.RegisterL2CapCallback(l2capCallbackFunc);

Peripheral.L2CapMgr.EstablishChannel(l2CapChannelInfo);

Note that we have registered callback named l2capCallbackFunc. You will receive indications when a particular event has occurred inside the callback functions. The values provided are not actual values, please modify as per requirement.

Here's how you can create the callback function. Write the following lines of code:

class l2capCallbackFunc : CyL2CapMgrCallback

        {

            public override void OnChannelEstablished(ICyL2CapChannel channel, CyStatus status)

            {

                base.OnChannelEstablished(channel, status);

            }

            public override void OnDataReceived(CyL2CapDataReceivedInfo info)

            {

                base.OnDataReceived(info);

            }

            public override void OnSendData(ushort channelID, CyStatus status)

            {

                base.OnSendData(channelID, status);

            }

        }

Note that I have just written a couple of events, there are others too, check documentation. You can work on the data received inside these functions.

So, when you call Peripheral.L2CapMgr.SendData somewhere in your code, the callback OnSendData is called where you can check the status of that transaction in that channel.

Hope this helps

Regards,

Dheeraj

New Contributor

Your answer is very helpfull. Now, I'm stuck with SendData. This fonction need CyL2CapSendDataInfo, that need ICyL2CapChannel. Can you explain how to obtain ICyL2CapChannel ?

Pascal

0 Likes
Reply
New Contributor

Can you help me to declare and use ICyL2CapChannel ?

Regards.

Pascal

0 Likes
Reply
Moderator
Moderator

Hello Pascal,

Here's how you use the SendData function.

Create a global instance variable of ICyL2CapChannel as shown below:

// Peer BLE device

ICyBleDevice Peripheral;

public static ICyL2CapChannel cyL2CapChannel;

In the previous interaction, I explained how you need to call EstablishChannel API.

CyEstablishL2CapChannelInfo l2CapChannelInfo = new CyEstablishL2CapChannelInfo(0, 0, 512, 10, 1000);

CyL2CapMgrCallback l2capCallbackFunc = new CyL2CapMgrCallback();

Peripheral.L2CapMgr.RegisterL2CapCallback(l2capCallbackFunc);

Peripheral.L2CapMgr.EstablishChannel(l2CapChannelInfo);

When the EstablishChannel API is called, a callback method OnChannelEstablished is executed when the channel is established.

You can check the status of this operation and if successful, pass the established channel info to your variable like this.

class l2capCallbackFunc : CyL2CapMgrCallback

{

     public override void OnChannelEstablished(ICyL2CapChannel channel, CyStatus status)

     {

        if (status == CyStatus.BLE_STATUS_OK) {

           cyL2CapChannel = channel;

         }

      }

      public override void OnDataReceived(CyL2CapDataReceivedInfo info)

      {

          base.OnDataReceived(info);

      }

      public override void OnSendData(ushort channelID, CyStatus status)

            {

                base.OnSendData(channelID, status);

            }

        }

You can then use this in your SendData function in this fashion:

CyL2CapSendDataInfo l2CapSendDataInfo = new CyL2CapSendDataInfo(cyL2CapChannel, BitConverter.GetBytes(0x001));

Peripheral.L2CapMgr.SendData(l2CapSendDataInfo);

Once the data is sent, you can check the callback method OnSendData for the status of the transaction as well.

Hope this helps

Regards,
Dheeraj

View solution in original post

Top labels