Wednesday, September 05, 2007

Get cell broadcast

Header file
#ifndef __CBUTIL_H__
#define __CBUTIL_H__

#include <e32base.h>
#include <Etel3rdParty.h>

#include <etelmm.h>
#include <etel.h>
#include <etelmmcs.h>
#include <flogger.h>
#include <csmshandler.h>

class CCBUtil : public CActive
{
public:
CCBUtil();

static CCBUtil* NewL();

void ConstructL();

~CCBUtil();

private:
void RunL();

void DoCancel();

private:
RFileLogger iLog;

TRequestStatus iReqStatus;

RTelServer iServer;

RMobilePhone iPhone;

RMobileBroadcastMessaging iBroadcastMsg;

TBuf8<88> iGsmMsgdata;

CSmsHandler* iSmsHandler;

};

#endif // __CBUTIL_H__

Source File
#include "cbutil.h"
#include <eikenv.h>
#include <Etelmm.h>
#include <Etel3rdparty.h>
#include <stdio.h>

_LIT(KGsmModuleName,"phonetsy.tsy");

CCBUtil::~CCBUtil()
{
Cancel();
iPhone.Close();
iServer.UnloadPhoneModule(KGsmModuleName);
iServer.Close();
iLog.CloseLog();
iLog.Close();
}

CCBUtil* CCBUtil::NewL()
{
CCBUtil* self = new (ELeave) CCBUtil();
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop();
return self;
}


void CCBUtil::ConstructL()
{
RTelServer::TPhoneInfo iPhoneInfo;
iServer.Connect();
iServer.LoadPhoneModule(KGsmModuleName);
TInt enumphone;
User::LeaveIfError(iServer.EnumeratePhones(enumphone));
if (enumphone < 1)
{
User::Leave(KErrNotFound);
}
User::LeaveIfError(iServer.GetPhoneInfo(0, iPhoneInfo));
User::LeaveIfError(iPhone.Open(iServer,iPhoneInfo.iName));
iBroadcastMsg.Open(iPhone);
RMobileBroadcastMessaging::TMobileBroadcastAttributesV1 iAttrInfo;
TPckg<RMobileBroadcastMessaging::TMobileBroadcastAttributesV1> iDes(iAttrInfo);
iBroadcastMsg.ReceiveMessage(iStatus,iGsmMsgdata,iDes);
SetActive();
}

CCBUtil::CCBUtil()
: CActive(EPriorityNormal)
{
CActiveScheduler::Add(this);
}

void CCBUtil::RunL()
{
if(iStatus==KErrNone)
{
RFs fs;
fs.Connect();
RFile file;
TBuf<32> aFileName = _L("C:\\log.txt");
fs.Delete(aFileName);
file.Replace(fs,aFileName,EFileWrite);
file.Write(iGsmMsgdata);
file.Close();
fs.Close();

FILE* fp;
fp = fopen("c:\\log.txt","rb");

char locationString[94];
char cbuf;
int char_cnt = 0;
unsigned int bb = 0;

unsigned char ur, curr, prev = 0;
int cnt = 0;
for ( cnt = 0; cnt < 6; cnt++)
fread(&cbuf, 1, 1, fp);

while(fread(&cbuf,1,1,fp))
{
unsigned char aa = (1<< (7 -bb%7))-1;
ur = cbuf & aa;
ur = (ur << (bb)) | prev;
curr = cbuf &(0xff ^ aa);
curr = curr >>(7-bb);
prev= curr;
if(ur == 0xd)
{
break;
}

locationString[char_cnt] = ur;
bb = ++bb % 7;
char_cnt++;
if ( bb== 0 )
{
locationString[char_cnt++] = prev;
prev =0;
}
}

locationString[char_cnt] = '\0';
fclose(fp);
int len = 0;
while (locationString[len] != NULL )
len++;

HBufC* nameHeap = HBufC::NewLC(len);
TPtr namePtr(nameHeap->Des());
TBuf<250> messageText;

for (int i = 0; i < len; i++)
namePtr.Append((TChar)locationString[i]);

messageText.Copy(namePtr);

CAknInformationNote* informationNote = new ( ELeave )
CAknInformationNote;
informationNote->ExecuteLD(messageText);

CleanupStack::PopAndDestroy(nameHeap);
}
else
{
CAknInformationNote* informationNote = new ( ELeave )
CAknInformationNote;
informationNote->ExecuteLD(_L("Error :p"));
}
iBroadcastMsg.Close();
}

void CCBUtil::DoCancel()
{
}

Yes, i admit that the above code does not work every time. If you have some fix to above code please send the fix to me at my email id skumar.mobiledev@gmail.com

1 comment:

Mat Dodgson said...

I got the same code somewhere however two of the includes are missing:

#include etelmmcs.h
#include csmshandler.h

Do you have them? If yes, can you put them somewhere or email them? (matdodgson at_ gmail.com)

stats counter