Posted in C#, Dotnet, General, Tools

Copy files from latest modified folders – Batch script

Hello Reader,

Part of a tool making process, i had a sub task, where i need to copy certain files from a few (in this case 3 in numbers) directories from recently modified directories (date-time descending sort) onto another path where again a latest single directory needs to be searched, under which the predefined files are copied to.

In other words and detailed: A build machine runs its scheduled job, creates few files (if failure) in a 3 different folders: components/modules wise, the names are not in fixed structure. This central repository shall have daily 3 folders added into it.

Hence, i was in need of a batch script where it gets the latest (descending sorted) modified 3 folders and inside each of them, search for few files (pre-defined), when found, does a blind copy onto a destination path: where again with a date-time descending sort, latest folder is found (just 1); which i consider as Output folder of all logs.

Later on, another tool looks for these few pre-defined files to determine the failure condition of the build and generates few reports. WIth few hours of looking all over google, i found very minimal solution available nor suitable as per my needs and then reading quite a good documents of windows commands and understanding it, i could come up with below script.

scree

Code description:

Line 4, is required for the below FOR loop where in we need to retain the state of the variables and update it all through the loop.

Line 12, is the loop where in i am using /f argument to iterate over the out of the command executed in the loop header, in this case its DIR command. In this case, i need only the Folders which are non-hidden and non-system folders needs to be sorted in descending order based on modified time. Hence the argument /ad-h-s /TW /O-d. More details can be found in the help document of Dir command.
Line 12, loop shall first find out the single latest directory in the destination path where a files needs to be copied out.

Line 19, loop sorts the source directory contents (folders only) in descending order based on modified datetime.  Then picks up first 3 top directories from it and copies files (if present) inside these 3 folders to destination directory found from loop in Line 12.
Since the state of the loopcount is retained via delayedexpansion concept, the accessing of the variable is done with !<variable>! than the normal %<variable>% way at Line 21. If done via traditional way, then the variable value when accessed wont be having the latest value in it.

Cheers! Zen

Posted in Linux

Bluetooth (ObexFTP) file transfer timeout on Linux

Dear Reader,

Lately i have been experiencing a weird issue on my Linux box of Zenwalk 7.8. I am using blueman app here to transfer the files.

Strange part was, i was able to send files to my linux box (Laptop) from my mobile device, but not the other way around. This always irked me a bit but did not bother much as i hardly use the bluetooth. But today, i decided to do a bit of research (logs and google) on this and i happen to run some diagnosis.

First, via blueman i paired the devices. Trying to send the file from box to mobile device, it reported time out error. Looking at the dmesg logs, it turned out nothing much useful.

Next, i went on to see if the device has been properly recognized by the bluetooth service. So ran few commands as root (sudo) user listed below:

bash-4.3# hcitool dev
Devices:
hci0    3C:XX:XX:XX:XX:XX

The MAC address printed from the above command is the bluetooth chip on my laptop/desktop.

To know HCI0 bus info, you can run command:

bash-4.3# hciconfig dev
hci0:    Type: BR/EDR  Bus: USB
BD Address: 3C:XX:XX:XX:XX:XX  ACL MTU: 1021:8  SCO MTU: 64:1
UP RUNNING PSCAN
RX bytes:15273 acl:156 sco:0 events:648 errors:0
TX bytes:109775 acl:248 sco:0 commands:419 errors:0

Once the device is detected, next i ran another command to make the device (BUS) up, in case it hasn’t.

bash-4.3# hciconfig hci0 up

The above command doesn’t print out any return lines. All is well so far.

Next i wanted to know if the mobile device is in the radar, i need to scan it:

bash-4.3# hcitool scan
Scanning …
7C:XX:XX:XX:XX:XX    zenwalker

Note the MAC address printed above is of the mobile device.

Then i wanted to connect the hcitool to mobile device:

bash-4.3# hcitool cc 7C:XX:XX:XX:XX:XX

Everything seems to be on track. Now, since the blueman is unable to transfer the file to my mobile device (Hint: Make sure your firewall isn’t blocking connection), its of no use using it further. So i tried via obexftp tool, since the blueman app uses the same tool underneath.

bash-4.3# obexftp -b 7C:XX:XX:XX:XX:XX -p /home/zenwalker/Pictures/abc.jpg

I tried sending a pic to my mobile device via above command, but it returned me some errors:

Connecting…failed: connect
Tried to connect for 3ms
error on connect(): Invalid argument

Further investigation revealed that, i need to explicitly mentioned the channel to which it should communicate. Usually this shall be figured by itself, but some times some mobile devices act crazy or the tool here itself is not bullet proof.

So what the heck is this channel or where do i get it?😮

Run this command:

bash-4.3# sdptool browse 7C:XX:XX:XX:XX:XX

This command outputs following result:

Service RecHandle: 0x10000
Service Class ID List:
“Generic Attribute” (0x1801)
Protocol Descriptor List:
“L2CAP” (0x0100)
PSM: 31
“ATT” (0x0007)
uint16: 0x1
uint16: 0x5

 

Service RecHandle: 0x10001
Service Class ID List:
“” (0x1800)
Protocol Descriptor List:
“L2CAP” (0x0100)
PSM: 31
“ATT” (0x0007)
uint16: 0x14
uint16: 0x1e

 

Service Name: Headset Gateway
Service RecHandle: 0x10002
Service Class ID List:
“Headset Audio Gateway” (0x1112)
“Generic Audio” (0x1203)
Protocol Descriptor List:
“L2CAP” (0x0100)
“RFCOMM” (0x0003)
Channel: 2
Profile Descriptor List:
“Headset” (0x1108)
Version: 0x0102

 

Service Name: Handsfree Gateway
Service RecHandle: 0x10003
Service Class ID List:
“Handsfree Audio Gateway” (0x111f)
“Generic Audio” (0x1203)
Protocol Descriptor List:
“L2CAP” (0x0100)
“RFCOMM” (0x0003)
Channel: 3
Profile Descriptor List:
“Handsfree” (0x111e)
Version: 0x0106

 

Service Name: AV Remote Control Target
Service RecHandle: 0x10004
Service Class ID List:
“AV Remote Target” (0x110c)
Protocol Descriptor List:
“L2CAP” (0x0100)
PSM: 23
“AVCTP” (0x0017)
uint16: 0x102
Profile Descriptor List:
“AV Remote” (0x110e)
Version: 0x0103

 

Service Name: Advanced Audio
Service RecHandle: 0x10005
Service Class ID List:
“Audio Source” (0x110a)
Protocol Descriptor List:
“L2CAP” (0x0100)
PSM: 25
“AVDTP” (0x0019)
uint16: 0x102
Profile Descriptor List:
“Advanced Audio” (0x110d)
Version: 0x0102

 

Service Name: Android Network Access Point
Service Description: NAP
Service RecHandle: 0x10006
Service Class ID List:
“Network Access Point” (0x1116)
Protocol Descriptor List:
“L2CAP” (0x0100)
PSM: 15
“BNEP” (0x000f)
Version: 0x0100
SEQ8: 0 6
Language Base Attr List:
code_ISO639: 0x656e
encoding:    0x6a
base_offset: 0x100
Profile Descriptor List:
“Network Access Point” (0x1116)
Version: 0x0100

 

Service Name: OBEX Phonebook Access Server
Service RecHandle: 0x10008
Service Class ID List:
“Phonebook Access – PSE” (0x112f)
Protocol Descriptor List:
“L2CAP” (0x0100)
“RFCOMM” (0x0003)
Channel: 19
“OBEX” (0x0008)
Profile Descriptor List:
“Phonebook Access” (0x1130)
Version: 0x0101

 

Service Name: OBEX Object Push
Service RecHandle: 0x10009
Service Class ID List:
  “OBEX Object Push” (0x1105)
Protocol Descriptor List:
  “L2CAP” (0x0100)
  “RFCOMM” (0x0003)
    Channel: 12
  “OBEX” (0x0008)
Profile Descriptor List:
  “OBEX Object Push” (0x1105)
    Version: 0x0100

Above is a series of channels available in my mobile for many types of communications. Search in your output for “OBEX file transfer” or “OBEX object push” or similar text under service name section (highlighted in RED), the rest all is gibberish related to this subject. Take a note of the Channel value. In my case its 12, for Nokia phones it could be 11 and for some other phones it could be something else.

Lets try again to transfer the file by explicitly stating the channel:

bash-4.3# obexftp -b 7C:XX:XX:XX:XX:XX  -B 12 -p /home/zenwalker/Pictures/abc.jpg

But again dead-end with errors:

Connecting..\failed: send UUID
Tried to connect for 120ms
error on connect(): Success
Still trying to connect

Back to drawing board😦. Further investigation revealed that i should force the obexftp tool to ignore UUID (hint taken from error message😀 )

So now, lets use options “-U NONE” for the above command:

bash-4.3# obexftp -b 7C:XX:XX:XX:XX:XX  -B 12  -U NONE -p /home/zenwalker/Pictures/abc.jpg
Suppressing FBS.
Connecting..\done
Tried to connect for 112ms
Sending “/home/zenwalker/Pictures/abc.jpg”…-done
Disconnecting..\done

Yuppie, at last success😀

Next step is to add these options into the bluetooth config files to make blueman work successfully. That’s for another blog post😉

Thanks, hope it helps
Zen:)

Posted in C#, Dotnet

Generic method to avoid long IF clauses

Dear Reader,

Recently at work, i was working with a library which had lot of properties wherein they must be verified for its Truth value before exporting or importing respective attributes to file or back to UI respectively.

Basically, the raw code initially looked roughly like below:

void ExampleMethod() {
SomeType type = new SomeType();

if (type.HasValue1)
  //Export Value1
if (type.HasValue2)
  //Export Value2
if (type.HasValue3)
  //Export Value3
if (type.HasValue4)
  //Export Value4
if (type.HasValue5)
  //Export Value5
if (type.HasValue6)
  //Export Value6
if (type.HasValue7)
  //Export Value7
if (type.HasValue8)
  //Export Value8
if (type.HasValue9)
  //Export Value9
if (type.HasValue10)
  //Export Value10
if (type.HasValue11)
  //Export Value11
if (type.HasValue12)
  //Export Value12

if (type.HasValue25)
  //Export Value25
}

The above case got repetitive at many places for other types in the same library based on level of their appearance in the UI viz. tree level node. The body of the if-clause all over the place remained same i.e 1 – Check for Truth, 2 – if success, Add to some final list for exporting/importing if not already present.

The method complexity in some cases went over 20 which was getting really gruesome to me. Finally, i decided to refactor the code to reduce not just the complexity but also find a unified solution for other basic types if need be.

Struck to me are 2 possible yet simple solutions yet satisfying the required criteria.

Solution 1: To use the Dictionary<Func< T >, SomeType>, but the body of all those if clauses required another 3rd parameter which hindered me to not opt the dictionary since Dictionary can’t have more than Key : Value pair thus making me to add dictionary inside dictionary which is not only complex in readability aspects but also Value dictionary may not be unique as well. So i had to discard this solution.

Solution 2: To make use of Generic Methods with delegates via lambda expression. This proved to be a working solution in this case yet very simple and effective.

void CheckAndDoSomething < T > (Func< T > property, object Value,
object otherValue) where T: struct
{
  Type t = typeof(T);
  if (t is bool && property())
  {
     //Do some thing with Value and OtherValue
  }
}

void ExampleMethod()
{
  SomeType type = new SomeType();
CheckAndDoSomething(() => type.HasValue1,value0, otherValue0);
CheckAndDoSomething(() => type.HasValue2,value1, otherValue1);
CheckAndDoSomething(() => type.HasValue3,value2, otherValue2);

}

Hope it helps,
Thanks,
Zen:)

Posted in General, Linux

Xiaomi Redmi File transferring with Linux.

Explained in simple steps:

1. You need ADB (Android Bridge) tools. Just paste this in your terminal as SU bash <(curl https://raw.githubusercontent.com/…/nexus…/master/install.sh)
2. The nexus tool downloads and install appropriate version of ADB for your Linux box. Wait few minutes.
3. As SU, execute => adb start-server
4. Enable USB debugging on ur RedMe and connect to your linux box.
5. Still as SU, execute => adb devices. It shall list your android device.
6. If you get “????????? no permission” as output from step 5, then you have not started adb service as SU and other commands too.
7. Once you got things straight. Now copy files from phone to pc via “adb pull” or paste to phone via “adb push”.
8. In my case, i wanted to copy entire DIR onto phone. So i did just “adb push /PC_DIR_Path/DIR_TO_COPY /PHONE/DIR_COPIED
9. To know the path on your phone, just use any file explorer and get the details of path. In my case it was /storage/sdcard1/Music. It could be different on yours.

Posted in General, Travel

GOA: Dream trip come true!

The background story goes like 4 years back, where in i had been waiting and planning to visit the exotic place GOA. Every time a plan is made, some or the other reasons the folks used to back out or some other problems. Yes it took 4 years for this damn plan to get executed. We bangloreans do really have lousy busy life…😐

The start of the new year, did bring in some good luck for me:) The plan got executed. We went for 5 beaches : Vagator, calangute, anjuna, baga and condolim.

The route suggested to me was : Belgaum – amboli ghat – sawantwadi – goa. Onward journey we took Belgaum – VTU – kankumbi – surla (chorla ghat) – mapusa by inquiring with local tea shop guys at Belgaum. To our surprise, this route was much much better than sawantwadi route which was our return journey.

On this route there is no lousy RTO check post or police trouble and even the goa police check post also doesnt bother much to the travellers. I rekon this route is mostly used by the local folks than others. The traffic on this route was very very minimal. The whole Chorla ghat section is around 50kms ODD. The road conditions on this ghat is brand new and awesome. I loved driving in this section compared to amboli in all senses.

Let pics do the talk…. First trip i did not care to take enough pics…

Onward journey

The onward journey road was soo good, i loved driving till Goa from Chitradurga. And the fact that its my babez (Punto), it just rushes out all the adrenaline you need:)

Apparently security did not let us in to see VTU upclose as no lights were on.

The early morning pit stop after Chorla Ghat section drive.

Palacete Rodrigues: The lousy hotel we stayed at Anjuna beach. As per Agoda.com website, this place suppose (pics wise) to have beautiful rooms, big pool and good food restaurant. Pool size is soo small that you hardly can toss around. The rooms size of 4 were lousy, the cots were bad, bed spreads dirty. The food was bad or next to horrible. I personally would rate this place 1. I heard that the couples suite costing 6+k/day is good but the ones which we stayed costed 3k, was not

Lovely view from Agoda Fort

Thats the agoda forts light house.

Sinquerim Rocks, Condolim beach. Beware there is not enough parking space here.

If lit well, Suvarna vidhana soudha should have been visible😦

Curries Bar & Restaurant : Very nice place outside calangute beach area and very nice food, good prices and rooms available too. I recommend this place for any body. They have couples bedroom, family (10 member) bedrooms too and very good prices. Foods are very reasonable prices too. We loved the food though with good quality. This guy Mahesh does very clean business. Infact our next visit to goa will def choose this place.
Address: Porba Vaddo, Calangute, Bardex, Goa 403 517. Email: write2maheshhh@gmail.com. Ph: 9822580711

Loved the food and stuffs here.

Posted in C#, CodeProject, Dotnet

Decision matrix implementation in C#

Dear Reader,

Yesterday i faced a problem where the combinations possibilities were many and i had to implement code for this problem. Obvious solution is to implement If – else ladder. But i absolute hate this solution because the combination were around 5C3. The combination data was about behavior aspect of a scenario. Next solution is to use Switch block. But this solution involves inner switch combinations. So down the line it gets more complicated.

Next solution was to go for dictionary. But the problem with dictionary is the key uniqueness. In my case, most of the keys were not unique. So either i had to customize the dictionary behavior (List of KeyValuePair) or go for some thing else. The earlier will lead to writing alot of code for small problem and involves more cost factor in terms of development time and testing effort.  Idea rejected.

Then i suddenly remembered to make use of Decision table. So googling a bit to learn more about decision matrix, this is what i could implement which is not just extensible but also easy to understand and easy to add/modify later on. No need to change in the actual code (finding the combo) unless the way to look up methodology changes.

Sample code:

Untitled

At line 27, i am here searching for a combination key of “Condition3” AND “ASC”, then get the result from the matrix. Line 31, have used FirstOrDefault() than First() to avoid exception when result is not found.

Hope it helps,
Thanks,.
Zen:)

Posted in Linux

Linux: gdm (XServer) Crash with error dgm_slave_xioerror_handler

Dear Reader,

Recently upgrading XServer, GDM with many other packages, suddenly launching gthumb started to crash the XServer. Resulting me to pop back into login window. This happen all the time consistently. At first i thought it was the problem with Gthumb application itself. Tracing back the upgrade log, i did not find itself getting upgraded in a long time. So next step was to see its dependent packages. From the .dep file in the FTP server, i saw there was huge number of packages on which gthumb is dependent on. So going this way in finding which was upgraded and which was is not really a good choice. So i immediately started to look into kernel messages aka dmesg and system logs files.

I did not find much information in dmesg. Next digging further into /var/log/syslog file prompted me to this error line:

gdm-binary[5197]: WARNING: gdm_slave_xioerror_handler: Fatal X error – Restarting :0

This line itself was enough to give me assurance that it was caused by the GDM and not any gtumb or related applications. Now GDM package involves even Xserver packages too. So googling was the only left out solution. As per google, there were many suggestions given i.e Reinstalling xserver packages, editing xorg.conf file, etc. None of them was much helpful.

Next is to look at the video driver side. So looked at the Nvidia Linux drivers page, found that a long stable version is released which is 319.49. I had 319.23. The new version release notes also mentions about a bug fix on Xserver/gdm crash. So immediately went for upgrading the driver.
Voila, it worked. Now i can use gthumb, gimp and other applications which were causing the crash.
Hope it helps…

Thanks,
Zen:)